How does a Java HashMap handle different objects with the same hash code?

前端 未结 14 1533
余生分开走
余生分开走 2020-11-22 02:27

As per my understanding I think:

  1. It is perfectly legal for two objects to have the same hashcode.
  2. If two objects are equal (using the equals() method)
14条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 03:20

    import java.util.HashMap;
    
    public class Students  {
        String name;
        int age;
    
        Students(String name, int age ){
            this.name = name;
            this.age=age;
        }
    
        @Override
        public int hashCode() {
            System.out.println("__hash__");
            final int prime = 31;
            int result = 1;
            result = prime * result + age;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            System.out.println("__eq__");
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Students other = (Students) obj;
            if (age != other.age)
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
    
        public static void main(String[] args) {
    
            Students S1 = new Students("taj",22);
            Students S2 = new Students("taj",21);
    
            System.out.println(S1.hashCode());
            System.out.println(S2.hashCode());
    
            HashMap HM = new HashMap (); 
            HM.put(S1, "tajinder");
            HM.put(S2, "tajinder");
            System.out.println(HM.size());
        }
    }
    
    Output:
    
    __ hash __
    
    116232
    
    __ hash __
    
    116201
    
    __ hash __
    
    __ hash __
    
    2
    

    So here we see that if both the objects S1 and S2 have different content, then we are pretty sure that our overridden Hashcode method will generate different Hashcode(116232,11601) for both objects. NOW since there are different hash codes, so it won't even bother to call EQUALS method. Because a different Hashcode GUARANTEES DIFFERENT content in an object.

        public static void main(String[] args) {
    
            Students S1 = new Students("taj",21);
            Students S2 = new Students("taj",21);
    
            System.out.println(S1.hashCode());
            System.out.println(S2.hashCode());
    
            HashMap HM = new HashMap (); 
            HM.put(S1, "tajinder");
            HM.put(S2, "tajinder");
            System.out.println(HM.size());
        }
    }
    
    Now lets change out main method a little bit. Output after this change is 
    
    __ hash __
    
    116201
    
    __ hash __
    
    116201
    
    __ hash __
    
    __ hash __
    
    __ eq __
    
    1
    We can clearly see that equal method is called. Here is print statement __eq__, since we have same hashcode, then content of objects MAY or MAY not be similar. So program internally  calls Equal method to verify this. 
    
    
    Conclusion 
    If hashcode is different , equal method will not get called. 
    if hashcode is same, equal method will get called.
    
    Thanks , hope it helps. 
    

提交回复
热议问题