javahashmaptrove4j

Setting default value in Trove hash map


How do I set the default value (returned when the key doesn't exist) for a TLongDoubleHashMap collection. It returns 0 by default, I would like it to return NaN.

I found this in the documentation:

Returns the value that will be returned from get(long) or put(long, double) if no entry exists for a given key. The default value is generally zero, but can be changed during construction of the collection.

But I have found no way to set the value during construction.


Solution

  • By using this constructor:

    public TLongDoubleHashMap(int initialCapacity,float loadFactor,long noEntryKey,double noEntryValue)
    

    For example:

    TLongDoubleHashMap map = new TLongDoubleHashMap(5, 0.5f, Double.NaN, Double.NaN)