Consistency of hashCode() on a Java string

I can see that documentation as far back as Java 1.2. While it’s true that in general you shouldn’t rely on a hash code implementation remaining the same, it’s now documented behaviour for java.lang.String, so changing it would count as breaking existing contracts. Wherever possible, you shouldn’t rely on hash codes staying the same across … Read more

Apache Commons equals/hashCode builder [closed]

The commons/lang builders are great and I have been using them for years without noticeable performance overhead (with and without hibernate). But as Alain writes, the Guava way is even nicer: Here’s a sample Bean: public class Bean{ private String name; private int length; private List<Bean> children; } Here’s equals() and hashCode() implemented with Commons/Lang: … Read more

Memory address of variables in Java

That is the class name and System.identityHashCode() separated by the ‘@’ character. What the identity hash code represents is implementation-specific. It often is the initial memory address of the object, but the object can be moved in memory by the VM over time. So (briefly) you can’t rely on it being anything. Getting the memory … Read more

What is the use of hashCode in Java?

hashCode() is used for bucketing in Hash implementations like HashMap, HashTable, HashSet, etc. The value received from hashCode() is used as the bucket number for storing elements of the set/map. This bucket number is the address of the element inside the set/map. When you do contains() it will take the hash code of the element, … Read more

Good Hash Function for Strings

Usually hashes wouldn’t do sums, otherwise stop and pots will have the same hash. and you wouldn’t limit it to the first n characters because otherwise house and houses would have the same hash. Generally hashs take values and multiply it by a prime number (makes it more likely to generate unique hashes) So you … Read more

Why use a prime number in hashCode?

Prime numbers are chosen to best distribute data among hash buckets. If the distribution of inputs is random and evenly spread, then the choice of the hash code/modulus does not matter. It only has an impact when there is a certain pattern to the inputs. This is often the case when dealing with memory locations. … Read more

How does a Java HashMap handle different objects with the same hash code?

A hashmap works like this (this is a little bit simplified, but it illustrates the basic mechanism): It has a number of “buckets” which it uses to store key-value pairs in. Each bucket has a unique number – that’s what identifies the bucket. When you put a key-value pair into the map, the hashmap will … Read more

How to get the unique ID of an object which overrides hashCode()?

System.identityHashCode(yourObject) will give the ‘original’ hash code of yourObject as an integer. Uniqueness isn’t necessarily guaranteed. The Sun JVM implementation will give you a value which is related to the original memory address for this object, but that’s an implementation detail and you shouldn’t rely on it. EDIT: Answer modified following Tom’s comment below re. … Read more