What's the use of a hash codes in Java? Java uses hash codes for the same reason described above-to efficiently retrieve data from hash based collections. If the objects of your class is not used as keys in a hash based collection, for example, in a Hashtable, HashMap, etc, You need isn't even worry about hash codes for your objects at all.
You can compute hash code for a object in Java. In case of a object, the pieces of information that would be is used to compute the hash code is the pieces of information t Hat make up the state of the object. Java designers considered the hash code for an object so important it they provided a default implementation to compute The hash code for a object in the object class.
The object class has a hashcode () method, which returns an int, and which is the hash code of the object. The default implementation of this method computes the hash code of a object by converting the memory address of the Obje CT to an integer. Since the Hashcode () method is defined in the Object class, it's available in all classes in Java. However, you is free to override the implementation in your class. Here is the rules that you must follow when you override the Hashcode () method in your class. Suppose there is object references, x and Y.
If x.equals (Y) returns True, X.hashcode () must return an integer, which is equal to Y.hashcode (). That is, if both objects is equal using the Equals () method, they must has the same hash codes.
If X.hashcode () is equal to Y.hashcode (), it's not necessary that X.equals (Y) returns True. That is, if the objects has the same hash codes using the Hashcode () method, they do not has to be equal using the equal S () method.
If the Hashcode () method is called on the same object multiple times in the same execution of a Java application, the Meth OD must return the same integer value. The hashcode () and Equals () methods are closely tied. If your class overrides any of these and methods, it must override both for the objects of your class to work correctly in Hash-based collections. Another rule is this should use only those instance variables to compute the hash code for a object, which is also u Sed in the Equals () method to check for equality.
If your class is mutable, you should the not being using objects of your class as keys in hash-based collections. If The object has been used as a key changes after their use, you are not being able to locate the object in the collection Because locating an object in a hash based collection are based on its hash code. In such cases, you'll have stranded objects in the collection.
How should implement a Hashcode () method for a class? Here's some guidelines to write the logic for the Hashcode () method for your class, which was reasonable for most of the Purposes:
Start with a prime number, say 37.
int hash = 37;
Compute the hash code value for each instance variable of primitive data types separately using the following logic. Note that you need-those instance variables in the hash code computation, which is also part of the Equals () Method logic. Let's store the result of this step in an int variable code. Let's assume that value is the name of the instance variable.
For byte, short, int, and char data types, use their integer value as
Code = (int) value;
For long data type, with the XOR for halves of 64-bit as
Code = (int) (value ^ (value >>>32));
For float data type, convert it floating-point values to an equivalent integer value using
Code = float.floattointbits (value)
For double data type, convert it floating-point value to long using the Doubletolongbits () method of the Double class and Then convert the Long value to a int value using the procedure as described above for the Long data type.
Long longbits = double.doubletolongbits (value);
Code = (int) (longbits ^ (longbits >>>32));
For the Boolean data type, use 1 for true and 0 for false.
Code = (value? 1:0)
For a reference instance variable, use 0 if it is null. Otherwise, call it Hashcode () method to get its hash code. Suppose ref is the name of the reference variable.
Code = (ref = = null? 0:ref.hashcode ());
Compute the hash code using the following formula. Using the formula is an arbitrary decision. Any other prime number, say, would work fine.
hash = hash * + code;
Repeat the above three steps for all instance variables are want to include in your hashcode () computation.
Finally, return the value contained in the hash variable from your hashcode () method.
The above method is a one of the many ways, not the only, and to compute hash code of a object in Java. Consult a good textbook on computing hash Codes if you need a stronger hash function. All primitive wrapper classes and String class override the Hashcode () method to provide reasonably good implementations O F hash functions.
Tip Java 7 added a utility class java.lang.Objects. It contains a hash () method that is computes the hash code for any number of values of any type. From Java 7, you is advised to use the Objects.hash () method to compute the hash code of an object. Please refer to "The Objects Class" sections later in this chapter for more details.
Hash codes in Java i:gneral usage and what to produce hash codes