Interview Questions

Java Keywords
Key Concepts
Key Concepts #2
OOPS in java
Java Collections#1
Java Collections #2
Exceptions #1
Exceptions #2
Immutable Class
Cloning in Java
Garbage Collection
JSP #1
JSP #2
Programming Q#1
Programming Q#2
Programming Q#3

Use of hashCode() and equals().

Object class provides two methods hashcode() and equals() to represent the identity of an object. It is a common convention that if one method is overridden then other should also be implemented.

Before explaining why, let see what the contract these two methods hold. As per the Java API documentation:

  • 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 hashtables.

Now, consider an example where the key used to store the in Hashmap is an Integer. Consider that Integer class doesn't implement hashcode() method. The code would look like:

  map.put(new Integer(5),"Value1?);
String value = (String) map.get(new Integer(5));
//Output : Value is null
Null value will be displayed since the hashcode() method returns a different hash value for the Integer object created at line 2and JVM tries to search for the object at different location.

Now if the integer class has hashcode() method like:

public int hashCode(){
return value;

Everytime the new Integer object is created with same integer value passed; the Integer object will return the same hash value. Once the same hash value is returned, JVM will go to the same memory address every time and if in case there are more than one objects present for the same hash value it will use equals() method to identify the correct object.

Another step of caution that needs to be taken is that while implementing the hashcode() method the fields that are present in the hashcode() should not be the one which could change the state of object.

Consider the example:

public class FourWheeler implements Vehicle {
      private String name;
      private int purchaseValue;
      private int noOfTyres;
      public FourWheeler(){}
public FourWheeler(String name, int purchaseValue) {
   = name;
            this.purchaseValue = purchaseValue;
      public void setPurchaseValue(int purchaseValue) {
            this.purchaseValue = purchaseValue;
      public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            result = prime * result + purchaseValue;
            return result;

//Try running this snippet
FourWheeler fourWObj = new FourWheeler("Santro","333333);
//Output: null

We can see that inspite of passing the same object the value returned is null. This is because the hashcode() returned on evaluation will be different since the purchaseValue is set to 555555" from 333333".

Though the above implementation is correct but it fails because for generating hashcode a changeable property (in this case price) is selected. To make above implementation correct we can either exclued the property or include some other property like noOfTyres and keep the logic of implementation same.

Hence we can conclude that the hashcode() should contain fields that doesn't change the state of object.

One compatible, but not all that useful, way to define hashCode() is like this:

public int hashcode(){
return 0;
This approach will yield bad performance for the HashMap. The conclusion which can be made is that the hashcode() should(not must) return the same value if the objects are equal. If the objects are not equal then it must return different value.

Continue reading>>