Java SE 045 hashCode and equals in-depth analysis and detailed source code

(1) As long as a person does not give up on himself, the whole world will not give up on you.
(2) I am born to be of great use . (3) If I
cannot bear the suffering of learning, I must bear the suffering of life. How painful it is Deep comprehension.
(4) You must gain from doing difficult things . (
5) Spirit is the real blade.
(6) Conquering opponents twice, the first time in the heart.
(7) Writing is really not easy. If you like it or have something for you Help remember to like + follow or favorite~

Java SE 045 hashCode and equals in-depth analysis and detailed source code

1.Set features

(1) No order of elements
(2) Elements cannot be repeated.

## 2. About the characteristics of the equals method of the Object class
(1) Reflexive: if object is not empty, x.equals(x) should be true.
(2) Symmetry: x.equals(y) is true, then y .equals(x) is also true;
(3) Transitivity: x.equals(y) is true, and y.equals(z) is true, then x.equals(z) should also be true;
(4) consistent Property: x.equals(y) is true, then the second, third, and nth calls of x.equals(y) should also be true, provided that the comparison does not modify x nor y.
( 5) For non-null reference x, x.equals(null) returns false;

2.1 Attention

(1) The equals() method of Object uses == to compare.
(2) When we want the Override equals() method, we must also go to the Override hashcode() method.

3. About the characteristics of the hashCode method of the Object class

(1) During one execution of a java application, for multiple calls to the hashCode method of the same object, they should return the same value (provided that the object's information has not changed).
(2) For two objects, if the equals method is used to compare and return true, then the hashCode value of the two objects must also be the same.
(3) For two objects, if the equals method is used to compare and return false, then the hashCode of the two objects is not required to be different (can be the same or different), but if they are different, the performance of the application can be improved.
(4) For the Object class, the hashCode value of different Object objects is different (the hashCode value of the Object class represents the address of the object).

4. HashSet call process

(1) When using HashSet, the hashCode() method will be called to determine whether the hash code value of the object already stored in the collection is consistent with the hashcode value of the added object. If it is inconsistent, add it directly. Compare the equals method. If the equals method returns true, it means that the object has been added, and no new object will be added, otherwise it will be added.

5. Summary

(1) For a collection, judging whether an object can be placed in the collection is done through the hash code and equals methods.

(2) If we want to have the same content for a class of our own, for example, if two people have the same name, its content will be the same. At this time, two Zhang San should not be placed in the same set.

Rewrite the hashcode method and the equals method, because these two methods are defined by the Object class, and these two methods are closely related, so in most cases, we only need to rewrite the hashcode method, we It is necessary to rewrite the equals method at the same time. The reverse is also the same. Rewrite the equals method, and also rewrite the hashcode method.

(3) If we rewrite the equals method, then also rewrite the hashCode method, and vice versa.

package com.javase.hashset;

import java.util.HashSet;
/**
 * 不使用Object类提供的hashCode方法与equalse方法,
 * 使用自己实现的hashCode方法与equals方法判断两个对象的内容是否相同
 * 重写hashCode方法与equals方法
 * @author x_xiongjie
 *
 */
public class SetTest3 {
    
    
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
    
    
		HashSet set = new HashSet();
		Student s1 = new Student("zhangsan");
		Student s2 = new Student("zhangsan");
		set.add(s1);
		set.add(s2);
		System.out.println(set);
		
	}
}

class Student{
    
    
	private String name;
	
	public Student(String name){
    
    
		this.name = name;
	}
	//借助于name的hash code来代表类本身的hash code.
	@Override
	public int hashCode() {
    
    
		return this.name.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
    
    
		if(obj == this){
    
    
			return true;
		}
		//如果obj不为空,并且是当前类的实例。
		if(obj != null && obj instanceof Student){
    
    
			Student s = (Student) obj;
			if(name.equals(s.name)){
    
    
				return true;
			}
		}
		return false;
	}
}

(1) In the actual development process, judging whether the object can be placed in the collection, in most cases is determined based on the content, rather than the address.

(2) In actual implementation, both hashCode and equals are implemented through content. How this content is reflected is through the member variables of the class. Whether the content of the two objects is the same is reflected through member changes. Yes, the data represents the state of the object.
) In actual implementation, both hashCode and equals are implemented through content. How this content is reflected is through the member variables of the class. Whether the contents of the two objects are the same is reflected through member changes. The data represents the state of the object.

Guess you like

Origin blog.csdn.net/xiogjie_67/article/details/108540794