Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Hash32 and Hash in Java string Object [duplicate]

Tags:

java

string

What is the Difference between Hash32 and Hash in String Object

While Debugging I have found the String Object displays hash=0 and hash32=0 as shown in below image.Some could please explain the reason it is displayed.

enter image description here

Thanks for the Reply

like image 669
Java Beginner Avatar asked May 29 '14 05:05

Java Beginner


People also ask

Is hashCode same for two objects?

1) If two objects are equal (i.e. the equals() method returns true), they must have the same hashcode. 2) If the hashCode() method is called multiple times on the same object, it must return the same result every time. 3) Two different objects can have the same hash code.

What will happen if two different objects have same hashCode in HashMap?

HashCode collisions Whenever two different objects have the same hash code, we call this a collision. A collision is nothing critical, it just means that there is more than one object in a single bucket, so a HashMap lookup has to look again to find the right object.

What is HashMap and hashCode in Java?

In HashMap, hashCode() is used to calculate the bucket and therefore calculate the index. equals() method: This method is used to check whether 2 objects are equal or not. This method is provided by the Object class. You can override this in your class to provide your implementation.

Can two keys have same hashCode in Java?

It's perfectly legal for two unequal objects to have the same hash code. It's used by HashMap as a "first pass filter" so that the map can quickly find possible entries with the specified key. The keys with the same hash code are then tested for equality with the specified key.


1 Answers

The performance advantages of HashMaps can be "negated" by feeding it carefully chosen keys, so that you get lots of collisions. This reduces the O(1) access/whatever time for a HashMap to O(n), significantly reducing performance.

hash32 is the cached value for an alternative hashing algorithm, used for Strings, so that if there were too many collisions with the default algorithm a different algorithm could be used to try to reduce the number of collisions. Source here:

Java SE 7u6 introduces an improved, alternative hash function...

The alternative hash function improves the performance of these map implementations when a large number of key hash collisions are encountered.

The alternative hash function is only applied to keys of type String.

HashMap was rewritten in Java 8 to use ad-hoc TreeMaps for Comparable keys if there were too many collisions for any particular bucket, meaning that performance went from O(1) to O(lg n) instead of O(1) to O(n) -- a significant improvement. As Strings implement Comparable, it was decided that the alternative hashing scheme was no longer needed, and it was removed.

like image 147
awksp Avatar answered Sep 24 '22 18:09

awksp