Hashcode () equals () in Java is a problem to consider when putting an object into a collection or a map __java

Source: Internet
Author: User

The Hashcode () method of object in Java is not overridden, then the Hashcode () method returns the memory address ...

The ToString () method, as defined by the default object object, returns the Hashcode () return value of the package name + class name +@+16.


In our actual development, for example, the author defines a Java Bean object, and if I put it into the collection, a Java bean I think is the same as the Name property, so do not put it into the collection, I think it is a duplication of things.


Import java.util.*;
        
        public class Main {public static void main (string[] args) {Student student1 = new Student ();
        
        Student Student2 = new Student ();
        Student1.setpassword ("John");
        
        Student2.setpassword ("Dick");
        Map<student, string> map = new hashmap<student, string> ();
        Map.put (Student1, "student1");
        
        Map.put (Student2, "Student2"); /** *----value out or student2 because the uniqueness of the key in the map is based on this, first see if you put the object Hashcode () method is the same, if not the same directly, * if the same, to use equals comparison m
         The existing key in the AP looks the same, and if the Equals () method returns False, it is also added to the map, and if the true,map is put back, the item that is added to the duplicate key before it is flushed off, so the value that is placed in is returned.
        
        
        * * * * */SYSTEM.OUT.PRINTLN (Map.get (student1));
         /** * If you put an object into set, set is unique, he and map key is a truth, but first put in a, and then put in if the difference, then directly put in, the same will not be put in, and the same key in the map before the putting of the key value is not the same.
        * * set<student> Set = new hashset<student> (); SEt.add (STUDENT1);
        Set.add (Student2);
    
        System.out.println (Set.size ());
        for (iterator<student> iterator = Set.iterator (); Iterator.hasnext ();)
            {///print out the password is: John prove that later objects are not put in ....
        System.out.println (Iterator.next (). GetPassword ());
    
    }} class Student {String name;
    
    String password;
    Public String GetPassword () {return password;
    } public void SetPassword (String password) {this.password = password;
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    Public Student () {this.name = "AAA";
    @Override public int hashcode () {return this.name.hashCode (); @Override public boolean equals (Object obj) {if (this = = obj) {return true
        ;
        
}        if (null!= obj && obj instanceof Student) {Student s = (Student) obj;
            if (This.name.equals (S.getname ()) {return true;
    return false; }
}


In the set, how do we judge him to be "the same", the logic is,

First look at this Java Bean's Hashcode () method returns the same integer. If different, the key in the collection or map is placed directly.

If the same is the case, then the Equals () method is used to determine if the Equals () method returns false and adds it to the key in the collection or map.

If the Hashcode () method returns the same value as the key in the collection or map, and the Equals () method returns True. Then not at this time.

The set is not able to be put in, the map is the time of the previously placed Key-value to flush off.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.