Search code examples
javatrove4j

TObjectIntMap (Trove 3) How can 'int get(java.lang.Object key)' method return null? Is it a documentation mistake?


I have been looking into the documentation of Trove4j library. Particularly the TObjectIntMap interface. According to the documentation of the library the get method returns null if the key is not present in the map. The documentation I am referring to can be seen here:

http://trove4j.sourceforge.net/javadocs/gnu/trove/map/TObjectIntMap.html#get%28java.lang.Object%29

At the time of reading (27/02/2014) the documentation states

int get(java.lang.Object key) "Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key."

My question is:

How is it possible for the method to return null when the return type is the primitive int. As far as I know it is only possible for Integer references to be null. Is it a documentation error?


Solution

  • You are right, int can't be null. Thus their documentation is inconsistent.

    To give you a bit of insight how Trove is built: they have templates for all primitive and object permutations of datastructures, so it is understandable that you can't keep everything consistent. You should report this to the project however so they can fix that.

    To check if there is a value for a key, you should use the method

    public boolean containsKey(java.lang.Object key)
    

    or (a more hacky solution) is to check for the default value for no entries:

    gnu.trove.impl.Constants.DEFAULT_INT_NO_ENTRY_VALUE
    

    which defaults to 0, if you didn't override the gnu.trove.no_entry.int property.