gogoWebsite

How to rewrite hashCode and equals methods in java

Updated to 9 days ago

Conclusion: We all know that if the reference type is to be used as the key or set value of hashmap, the hashCode() and equals methods need to be overridden.

So how to rewrite hashCode() and equals methods and what are the techniques?

Each class in Java has defaults, because all classes are subclasses of the Object class. You can view the default implementation of the hashCode() and equals methods of Object. The code is as follows:

    public native int hashCode();

    public boolean equals(Object obj) {
        return (this == obj);    
    }



    

The hashCode() method calls the native method at the bottom of c. Equals() defaults to indicate whether the storage addresses of the two objects are the same.

2. Test

Prepare entity class:

static class User{
        public int getAge() {
            return age;
        }

        public void setAge(int age) {
             = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
             = name;
        }

        public String getIdNum() {
            return idNum;
        }

        public void setIdNum(String idNum) {
             = idNum;
        }

        private int age;
        private String name;
        private String idNum;

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof User) {
                User user = (User) obj;

                if (().equals() && ().equals() && () == ) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
        }

Use equals to test whether it is equal:

public static void main(String[] args){
        User user1 = new User();
        (10);
        ("Alice");
        ("000000000001");

        User user2 = new User();
        (10);
        ("Alice");
        ("000000000001");


        ("(user2) == "+(user2));
    }

Output:

(user2) == true

Use Map and Set to test whether it can be used as a key:

public static void main(String[] args){
        User user1 = new User();
        (10);
        ("Alice");
        ("000000000001");

        User user2 = new User();
        (10);
        ("Alice");
        ("000000000001");


        ("(user2) == "+(user2));

        Map<User,Integer> map = new HashMap<>();

        Set<User> set = new HashSet<>();

        (user1,10);
        (user2,10);

        (user1);
        (user2);

        ("().size() is: "+().size());

        ("() is: "+());
    }

Output:

(user2) == true
().size() is: 2
() is: 2

From this we can conclude that the hashCode referenced as the key of the map needs to be rewrite

After rewriting hashCode, the entity class User class is:

static class User{
        public int getAge() {
            return age;
        }

        public void setAge(int age) {
             = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
             = name;
        }

        public String getIdNum() {
            return idNum;
        }

        public void setIdNum(String idNum) {
             = idNum;
        }

        private int age;
        private String name;
        private String idNum;

        @Override
        public int hashCode(){
            int value = ();
            value = value*31 + ();
            value = value*31 + age;
            return value;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof User) {
                User user = (User) obj;

                if (().equals() && ().equals() && () == ) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }



        }

After the test, the size result is 1.