1

I am using trove library to create hash maps

http://trove.starlight-systems.com/ 

The class I am using is TObjectIntMap in which I had to use the function get. The issue is that get returns 0 if two cases

1- If the value of the specified key is zero

2- If the key does not exist

For example in the following code

TObjectIntMap<String> featuresMap = new TObjectIntHashMap<String>();

        if(String.valueOf(featuresMap.get("B")) == null)
            System.out.println("NULL");
        else 
            System.out.println("NotNull");


        System.out.println(featuresMap.get("B"));

The program will print the following

1- NotNull: because it gets zero. Although the key "B" has not been set

2- Zero: The return of featuresMap.get("B") is zero instead of null.

I have checked their documentation in the link below and it was a mistake that they solved. So get actually return zero instead of null because int cannot be null.

https://bitbucket.org/robeden/trove/issue/43/incorrect-javadoc-for-tobjectintmapget

Now my question is: How to differentiate between a zero and Null in this case. Is their any way around to address this issue.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Abdelrahman Shoman
  • 2,882
  • 7
  • 36
  • 61

1 Answers1

1

Try their containsKey method. If the value comes back 0, use that method to check if the map contains the key - if it does, then the key's value really is 0. If it doesn't, then the key is not set.

mk.
  • 11,360
  • 6
  • 40
  • 54
  • But do I really need to ckeck if return zero. I can only check "contains key", right ? – Abdelrahman Shoman Mar 02 '15 at 16:07
  • 1
    You get to pick the order. If it does contain a key, you might still need to get its value, in case you're interested in non-zero values. Glad I could help! – mk. Mar 02 '15 at 16:08
  • 2
    You can also adjust the "noEntryValue" (the value that's returned) by configuration from the constructor. So, for example, if -1 is a better value for you, that could be configured. See: http://trove4j.sourceforge.net/javadocs/gnu/trove/map/hash/TObjectIntHashMap.html#TObjectIntHashMap(int, float, int) – Rob Eden Mar 02 '15 at 16:22