SiteMap  | About    

HashCode and Equals method in Java

In Java, every object has access to the equals() method because it is inherited from the Object class. 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. You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode() , which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable. If you don't override hashcode() then the default implementation in Object class will be used by collections . This implementation gives different values for different objects, even if they are equal according to the equals() method.


The hashcode() is a method returns a hash code for this string. The 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.


This particular method is used to make equal comparison between two objects. There are two types of comparisons in Java. One is using "==" operator and another is "equals()". (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.