0

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.

Master Qiao
  • 301
  • 2
  • 10
  • If you will ever store `0` in your list, and you'll want to be able to differentiate between a slot having been set to that value vs having never been set to anything, then you should use a different value to represent no-entry. Will you ever store negative values? If not, `-1` would be a good choice. – CryptoFool Apr 01 '22 at 05:15

1 Answers1

1

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.

Rob Eden
  • 362
  • 2
  • 7
  • Whether does no_entry_value just work as a replacement in trove4j when its corresponding Java standard collections' methods need to return null, and not play any important role in trove4j' internal handling mechanisms, like work as a placeholder indicating a empty bucket. – Master Qiao Apr 01 '22 at 22:15
  • I'm sorry, I don't understand that question. Internally Trove has marker that indicates an empty slot, so it doesn't exactly rely on the no entry value (see [here](https://bitbucket.org/trove4j/trove/src/d4433e656b9eba5b1ed713ffe8e15660af37dfb4/core/src/main/templates/gnu/trove/impl/hash/_K__V_Hash.template#lines-228) for example). It's available mostly just for API purposes. – Rob Eden Apr 03 '22 at 01:14