Key Hashing in Java

In java, when I use String as a key for a Hashmap, I get a slightly different result than when I use a hashcode string as a key in a HashMap.

Any insight?

+4
source share
5 answers

when i use hashcode string as key in HashMap.

You should not use the hash code as a key. Hash codes are not intended to be unique - it completely allowed two unequal values ​​to have the same hash code. You must use the string as a key. Then the map will first compare the hash codes (quickly narrow down the candidates), and then compare with equals for true equality of strings.

Of course, assuming your code is really the way your question does, for example

 HashMap<String, String> goodMap = new HashMap<String, String>(); goodMap.put("foo", "bar"); HashMap<Integer, String> badMap = new HashMap<Integer, String>(); badMap.put("foo".hashCode(), "bar"); 

If this is really your code, just use HashMap<String, String> .

From the docs for Object.hashCode() (my attention):

General hashCode contract:

  • Whenever it is called by the same object more than once during the execution of a Java application, the hashCode method must consistently return the same integer if the information used in equal comparisons with the object does not change. This integer should not remain consistent with one execution of the application on another execution of the same application.
  • If two objects are equal in accordance with the equals (Object) method, then calling the hashCode method for each of the two objects should lead to the same integer result.
  • It is not required that if two objects are unequal according to the equals method (java.lang.Object), then calling the hashCode method for each of the two objects must produce different integer results. However, the programmer should be aware that creating separate integer results for unequal objects can improve the performance of hash tables.
+12
source

Sure. Different lines can have the same hash code, so if you store two lines like keys on the map, you will have two entries (since the lines are different). Whareas, if you use their hashCode as a key, you will only have one record (since their hashCode is the same).

A hash code is not used to determine if two keys are equal. It is used only to assign a bucket to a key. Once the bucket is found, each key contained in the bucket is compared with a new key with equal peers, and the key is added to the bucket if no keys are found.

+3
source

The problem is that even if two objects are different from each other, this does not mean that their hash codes are also different.

Two different objects can share the same hash code. Therefore, you should not use them as a HashMap key.

In addition, since the hash codes returned from the Object.hashCode() method are of type int , you can only have 2^32 different values. This is why you will have β€œcollisions” depending on the hashing algorithm for different objects.

In short: -

!obj.equals(obj1) does not guarantee that obj.hashCode() != obj1.hashCode() .

+3
source

HashCodes may be the same or different for the same String, so be careful with this. Perhaps that is why you get a different result.

Here is another SO question . See John Skeet's accepted answer.

+1
source

You can use a hash code as a key only if the hash function is an ideal hash (see, for example, GPERF ). As long as your key objects are not in memory, you are right to save memory.

0
source

Source: https://habr.com/ru/post/1443804/


All Articles