public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.
The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
a hash code value for this object.
here is the hash() method in HashMap:
The standard HashMap object has an array (Map.Entry) behind the scenes. It is arranged that the length of the array is always an exact power of 2, so the largest you are going to get is 2³⁰ = 1073741824 elements. When you exceed loadFactor × size (for the default hash map settings, 12 elements) the array is doubled in size and all the K‑V pairs rehashed and put into the new array. The old array is probably eligible for garbage collection. The Map uses the formula
h & c - 1
to determine the array index to put the K‑V pair; that location in the array is what you call a “bucket”. In that formula h is the rehashed hash code and c the capacity of the array and that formula is a bit like a remainder but it only works when c is an exact power of 2. It never gives a negative value. So that formula is exactly what you want to determine bucket indices. Remember that the − operator has a higher precedence than &. That formula never allows results larger than the capacity of the array provided c is a power of 2.
You cannot create an array larger than 2147483648 elements, not even if you have enough heap space. If you try to double the size from 2³⁰ = 1073741824 to 2³¹, you end up with 2147483648, which as you know is a non‑existent number for an int (it would overflow and turn negative). If you had enough memory to create such an array, you would suffer some sort of Exception.
raghu kalachar wrote:Suppose if user defined hashcode methods returns a value which is greater than integer range ...
That is not possible. The hashCode() method returns an int, so it can never return a value which is outside the range of values that an int can hold.
It is not directly HashMap with long hashes - rather Map with long keys. But it should be easy to built what you want upon it if it is not enough.
Would you need a lot of memory to sue that LongMap successfully?
Though I did not watch closely this thing during latest years...