Pre-requisite: Internal working of HashMap, HashMap
If we wish to create a HashMap of our own class, we need to ensure that the hashcode() of the key of HashMap doesn’t change as if it happens then it is impossible to get object value of the key from HashMap.
On runtime, JVM processes hash code for each object and give it on interest. When we alter an objects’ state, JVM calculates its hash code again which may result in memory leak. To make things work what we have to do is make sure that state change for a key object does not change the hash code of object i.e. the key must have properly overridden equals() and hashcode() methods for it to work correctly.
One of the ways of doing this is by making key objects IMMUTABLE. Immutability allows you to get same hashcode every time, for a key object. This is the primary motivation behind why Immutable classes like String, Integer or other wrapper classes are a decent key object applicant. Learn more about this here- How to make class Immutable?
| // Java example to create a Java HashMap// of user-defined class type importjava.util.*;importjava.io.*; // User defined classpublicclassCustomKeyObject {     publicstaticclassStudent {         privateintrollno;        privateString name;         // Constructor        publicStudent(introllno, String name)        {            this.rollno = rollno;            this.name = name;        }         publicString getname()        {            returnthis.name;        }         publicintgetmarks()        {            returnthis.rollno;        }         publicvoidsetname(String name)        {            this.name = name;        }         publicvoidsetmarks(introllno)        {            this.rollno = rollno;        }         // Overriding the hashcode() function        @Override        publicinthashCode()        {             // uses roll no to verify the uniqueness            // of the object of Student class             finalinttemp = 14;            intans = 1;            ans = temp * ans + rollno;            returnans;        }         // Equal objects must produce the same        // hash code as long as they are equal        @Override        publicbooleanequals(Object o)        {            if(this== o) {                returntrue;            }            if(o == null) {                returnfalse;            }            if(this.getClass() != o.getClass()) {                returnfalse;            }            Student other = (Student)o;            if(this.rollno != other.rollno) {                returnfalse;            }            returntrue;        }    }     // main method    publicstaticvoidmain(String[] args)        throwsNumberFormatException,               IOException    {        HashMap<Student, String> map = newHashMap<>();         Student one = newStudent(1, "Geeks1"); // key1         Student two = newStudent(2, "Geeks2"); // key2         // put keys in map        map.put(one, one.getname());        map.put(two, two.getname());         // changing key state so that        // hashcode() should be calculated again        one.setname("Not Geeks1");        two.setname("Not Geeks2");         // still prints Geeks1        System.out.println(map.get(one));         // still prints Geeks1        System.out.println(map.get(two));         // try with newly created key with same Rollno        Student three = newStudent(1, "Geeks3");         // we get Geeks1        System.out.println(map.get(three));    }     // This code is contributed by Subhesh} | 
Geeks1 Geeks2 Geeks1

 
                                    







