gogoWebsite

Which classes of java rewrite equals_rewrite equals and hashCode methods of java class

Updated to 18 days ago

1. What are the equals() method and hashCode() method?

equals() and hashCode() are both methods in the Object class, the source of everything in Java;

The equals method is used to compare whether two objects are the same. The implementation of the equals method in the Object class is to compare the reference address to determine whether the object is the same object. By overwriting this method, custom judgment rules can be implemented;

hashCode is a method in which jdk calculates the hash code value of the object based on the address, string or number of the object.

2. Why do we need to rewrite the equals() method?

The equals method in the Object class compares the reference addresses of two objects. Only when the reference address of the object points to the same address, do they think that the two addresses are equal, otherwise the two objects do not want to wait.

If there are two objects, their properties are the same but their addresses are different, the results obtained by using equals() comparison are not equal, and what we need is that these two objects are equal, so the default equals() method does not meet our requirements. At this time, we need to rewrite the equals() method to meet our expected results.

In Java's collection framework, the equals() method needs to be used to find objects. If the set stores a custom type and the equals() method is not overridden, the equals() method in the Object parent class will be called to compare according to the address, and there will often be error results. At this time, we should rewrite the equals() method according to business needs.

3. Why do you need to rewrite the hashCode() method?

The hashCode() method is used for fast storage of hash data. When the HashSet/HashMap/Hashtable class stores data, it is classified and stored according to the hashcode value of the stored object. Generally, it is first classified in the set based on the hashcode value, and judges whether the object is the same according to the equals() method.

The HashMap object obtains the corresponding value based on its key hashCode.

Generating a good hashCode value can improve the performance of HashSet searches. Poor hashCode value not only fails to improve performance, but may even cause errors. For example, returning constants in hashCode method will degrade the search efficiency of HashSet to the search efficiency of List collections; returning random numbers in hashCode method will make the search results unpredictable.

A good hashCode generation method is to multiply the key attributes in the object with prime numbers and add products to obtain.

4. Why must the hashCode() method be rewrite after rewriting the equals() method in Java?

In order to maintain the equals contract of the hashCode() method, the contract states that if two objects are equal according to the equals() method, then calling the hashCode method on each of the two objects must generate the same integer result; while the results returned by the two hashCode() are equal, and the equals() methods of the two objects are not necessarily equal.

The HashMap object obtains the corresponding value based on its key hashCode.

When overriding the equals() method of the parent class, the hashcode() method is also overridden to make the HashCode values ​​obtained by two equals objects also equal. In this way, when this object is used as a key in the Map class, the values ​​obtained by the two equals true objects are the same, which is more in line with reality.

The Person class code is as follows

Person{private intid;privateString name;privateString sex;

Omit get set

@Overridepublic booleanequals(Object o) {//Reflexivity

if (this == o) return true;//The object is empty, then it will not go down

if (o == null) return false;//Any object is not equal to null, compare whether it is the same type

if (!(o instanceof Person)) return false;//Captive type conversion

Person person =(Person) o;//Compare attribute values

return getId() == () &&(getName(), ())&&(getSex(), ());

}

@Overridepublic inthashCode() {int result =();

result= 17 * result +();

result= 17 * result +();returnresult;

}

}

5. How to understand the code in hashcode? How to write it?

In fact, there is a relatively fixed way to write it. First organize the attributes that you judge the object equal, and then take a positive integer as small as possible (as small as possible, I am afraid that the final result will exceed the range of integer ints). Here I took 17 (it seems that I have used 17 in the JDK source code), and then calculate the hashcode of 17* attribute + hashcode of other attributes, repeat the steps