This is the 26th day of my participation in the August Text Challenge.More challenges in August

equals() & hashCode()

I. First, a brief introduction to specific usage

  • Boolean equals(Object obj) – Determines whether the calling Object is equal to the parameter Object

    • By default, this method compares the addresses of two objects with those of = = The “equivalent \color{red} This method compares the addresses of two objects by default, equivalent to “==”
    • If you want to compare the contents of objects, you need to override the methods
    • If the method is overridden, then the hashCode() method should be overridden to maintain the general convention of the hashCode method
  • Int hashCode() – gets the hashCode value (memory address and number) of the calling object and returns it
    • If two objects call equals() equally, each call must yield the same hash code value
    • If two objects do not call equals() equally, the result of each call can be the same, but it is better not to be

Ii. A case study of equals() and hashCode()

  • First, we create a Student class
// Student class encapsulation
public class Student {

	private int id;
	private String name;
	
	
	public Student(a) {
		super(a); }public Student(int id, String name) {
		super(a);this.id = id;
		this.name = name;
	}
	
	public int getId(a) {
		return id;
	}
	public void setId(int id) {
		if (id > 0) {
			this.id = id;
		} else {
			System.out.println("Student number is unreasonable!"); }}public String getName(a) {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
Copy the code
  • Test in the TestStudent class
public class TestStudent {

	public static void main(String[] args) {
		
		Student s1 = new Student(1001."Wang");
		Student s2 = new Student(1001."Wang");
		
		System.out.println(s1 + "" + s2);
		// Call equals to determine whether two objects are the same, which is inherited from the Object class by default
		
		boolean b1 = s1.equals(s2);
		System.out.println("b1 = "+b1);  // b1 = false
		System.out.println(s1 == s2);  // false	}}Copy the code
Euqals.Student@2f92e0f4 Euqals.Student@28a418fc
b1 = false
false
Copy the code
  • Obviously, since the equals method compares addresses by default, s1 differs from S2, and the output is false. So what if we want two other objects to have the same Student id, and we want equals to output true? So, of course, equals belongs to the Object class, so we just override it in Student
// Student class encapsulation
public class Student {

	private int id;
	private String name;
	
	
	public Student(a) {
		super(a); }public Student(int id, String name) {
		super(a);this.id = id;
		this.name = name;
	}
	
	public int getId(a) {
		return id;
	}
	public void setId(int id) {
		if (id > 0) {
			this.id = id;
		} else {
			System.out.println("Student number is unreasonable!"); }}public String getName(a) {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		// 1. If two objects have the same address, they must have the same content
		if (this == obj) {
			return true;
		}
		// 2. If the call object is not null and the parameter object is null, the contents must be different (e.g. T1.equals (null)).
		else if (obj == null) {
			return false;
		}
		// 3. If the parameter object has the same type as the calling object, the student number determines whether the two objects have the same content
		else if (obj instanceof Student) {
			
			Student s = (Student)obj;
			if (this.getId() == s.getId()) {
				return true;
			} else {
				return false; }}// 4. If the type of the parameter object is different from that of the calling object, the content must be different
		else {
			return false; }}Copy the code
  • Here, we consider the case in 4. We run TestStudent again and find that we entered true because when we override equals, we call subclasses to override the later version and compare the content.
  • Here, we’re thinking of the String class, so what do we get when we call equals on strings?
System.out.println("Hello".equals("Hello"));  // true
Copy the code
true
Copy the code
  • We know that these are two strings. Shouldn’t the output be false? Color {red} equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals Is that it? No, not yet. As we said earlier, in order to maintain the general convention of hashCode methods, we override hashCode methods in the Student class
	// To ensure consistency with equals, we need to override hashCode
	@Override
	public int hashCode(a) {
		
		// return getId();
		
		// Whatever you want, to keep the ID the same
		// Maintain a general convention for the hashCode method
		int type = 12;
		return type*31 + getId();
	}
Copy the code
  • We test in the TestStudent class
		// call the hashCode method inherited from the Object class to get the corresponding hashCode
		// When a hashCode method is overwritten in the Student class, the overwritten version is called
		int res = s1.hashCode();
		System.out.println("res = " + res);
		
		res = s2.hashCode();
		System.out.println("res = " + res);
Copy the code
res = 1373
res = 1373
Copy the code
  • This is what we want. Do you Get it?

Finally, welcome to pay attention to my personal wechat public account “Little Ape Ruochen”, get more IT technology, dry goods knowledge, hot news