Skip to content
Advertisement

Should I care about no_entry_value in trove4j?

I’m using trove4j for its primitives collections. I notice it has constructors like

public TLongArrayList( int capacity, long no_entry_value )

where no_entry_value represents null. The default value is zero.

The null value in collections, like Set specially, is very important, according to my cognition. But I found trove4j did’t use this value much after I glanced at the source code.

So I’m confused that should I care about that value. Should I elaborately pick a value that would never occur in my programs, or just leave it to be default zero.

Advertisement

Answer

This is kind of one of those things that you know you need when you need it. It comes down to whether or not you want to just call get(...) and know whether or not the value was in the map without calling containsKey(...). That’s where the “no entry value” comes in because that is what is returned in the case where you didn’t store that key in your map. The default is 0, so if you never store a value of 0 in your map, that will work for checks. But of course that can be a bit risky. Typical values would be 0, -1, Integer.MAX_VALUE… things like that.

If there is no value that you can guarantee will never be in your map, then you need to make sure you check with containsKey before you trust the returned value. You can minimize the overhead of doing two lookups with something like:

int value = my_map.get( my_key );
// NOTE: Assuming no entry value of 0
if ( value == 0 && !my_map.containsKey( my_key ) ) {
   // value wasn't present
}
else {
   // value was present
}

That’s a performance improvement over calling containsKey every time before doing a get.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement