IDEA quickly rewrites equals() and hashCode() methods for Java Class

  There are some scenarios where you need to override the equals and hashCode methods of the Java class, such as when the Java object is used as the key of the Map. IDEA provides a convenient operation to quickly rewrite the above two methods. The process is briefly described as follows.

  Define a Java Class named: Student, the code is as follows.

package com.test.model;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Student {
    
    
    // 姓名
    private String stuNm;

    // 年龄
    private String age;

	// 构造函数
    public Student(String stuNm, String age) {
    
    
        this.stuNm = stuNm;
        this.age = age;
    }
}

  The process of quickly generating the above two methods is as follows: Press Alt + Ins in the Student.java file, call up Generate, and select "equals() and hashCode()".
Insert picture description here
  In the Generate equals() and hashCode() window, select "IntelliJ Default" Template.
Insert picture description here
  Select the attributes contained in the equals() method.
Insert picture description here
  Select the attributes included in the hashCode() method.
Insert picture description here
  Select all non-null attributes.
Insert picture description here
  Click the Finish button to quickly generate equals() and hashCode() methods. The generated Student.java file is shown below.

package com.test.model;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Student {
    
    

    // 姓名
    private String stuNm;

    // 年龄
    private String age;

    // 构造函数
    public Student(String stuNm, String age) {
    
    
        this.stuNm = stuNm;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (!(o instanceof Student)) return false;

        Student student = (Student) o;

        if (stuNm != null ? !stuNm.equals(student.stuNm) : student.stuNm != null) return false;
        return age != null ? age.equals(student.age) : student.age == null;
    }

    @Override
    public int hashCode() {
    
    
        int result = stuNm != null ? stuNm.hashCode() : 0;
        result = 31 * result + (age != null ? age.hashCode() : 0);
        return result;
    }
}

  If you do not override the equals() and hashCode() methods, when comparing referenced objects, the default comparison is the memory address of the object. After rewriting the method, as long as the name and age attribute value of the referenced object are equal, the two objects are considered equal. The test example is as follows.

package com.test.model;

import com.test.model.Student;

public class Test {
    
    

    public static void main(String[] args) {
    
    

        Student stuA = new Student("张三", "20");
        Student stuB = new Student("张三", "20");
        System.out.println(stuA.equals(stuB));                   // true
        System.out.println(stuA == stuB);                        // false
        System.out.println(stuA.hashCode() == stuB.hashCode());  // true
    }
}

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/107820523