Java Interview Questions

Java Interview Questions

Edit Content
Crack Your Interview

Object as a key in Hashmap – What are the constraints you will face?

When you want to use the Object of a custom class as a key in Hashmap, there are several things you need to consider, and that are

  • Override hashCode() and equals() in custom class.
  • The object of a custom class must be immutable.
1) Override hashCode() and equals() in custom class.
The reason behind overriding hashCode() and equals() in custom class is there is contract between hashcode() and equals() methods and that contract says if two objects are equal by equals() method (i.e. if both object’s content/field values) are same then there hashcode() must be the same but if both object’s hashcode() are same the in is not mandatory both objects are equal by equals() method.
 Let’s understand the use of this contract with below example, Suppose take an example, We have Student Class with id,Name and hashmap,
 
                        Student st1 = new Student(1, “Java”);
                        Student st2 = new Student(1, “Java”);
 
                         hashmap map= new Hashmap();
                         map.put(st1 , “One”);
                         map.put(st2,”Two”);
 
As We have override Override hashCode() and equals() in custom class, and st1.equals(st2) is true, both objects hashcode will be same. As Hashcode for the both objects are same both object will try to store at same bucket, so hash collision happens. In hash collision first we check if two objects are equal by equals() method and if it is equal, then new key will override with existing one as we can not store duplicate Keys in hashmap, an that is why we override hashcode() and equals() methods.
 
 2) The object of a custom class must be immutable. 
As We know the immutable object’s value or state can not be  changed. 
Suppose if we are using mutable class’s  object as key in hashmap, Lets try to understand this with an example.
 
                         Student st1 = new Student(1, “Java”);
                         hashmap map= new Hashmap();

                         map.put(st1 , “One”);

Now, as it is mutable we can easily change it’s value.

                       st1.setId(2);

                       st1.setName(“Spring”);

As we changes objects content, It’s hashcode is also chnaged, and now whenever you try to get the value for st1,

                       map.get(st1);

As per the working of hashmap, it will calculate hashcode and on the basis of it’s hashcode it’s index value will be determined, So now index value is something different like 11, so if 11 is empty we will get result as empty or wrong value instead of One, so that is the reason we must always use the immutable object as key.

Note : If you have any question you can add it in comment

Thank you!

 

3 thoughts on “Object as a key in Hashmap – What are the constraints you will face?”

Leave a Comment

Your email address will not be published. Required fields are marked *