Java-equal and hashCode, java-javashashcode
Come to the end of the paper, and never know how to do this-Lu YouWenqu that clear such, for the source of live water to Zhu Xi
Equals () is used to determine whether two objects are equal. In the Object class, equals (Object obj) checks whether the addresses of the two objects are equal (whether they are the same Object) to identify whether they are equal, source code:
public boolean equals(Object obj)
{
return (this == obj);
}
Because the Object class is the parent class of all classes, it is used to determine whether two objects are the same Object when equals (Object obj) is used in the subclass, the equals () method must be rewritten to determine whether the content of two objects is equal. Another common method is "=", and the equal sign is also used to judge the object reference.
Comparison between the example of rewriting equals () and the example of not rewriting equals:
Public class Hello {
Public static void main (String [] args)
{
Person1 person1 = new Person1 (10, "tian ");
Person1 person2 = new Person1 (20, "tian ");
If (person1.equals (person2 ))
{
System.Out. Println ("Equal ");
}
Else
{
System.Out. Println ("don't want to wait ");
}
If (person1 = person2)
{
System.Out. Println ("Equal ");
}
Else
{
System.Out. Println ("not equal ");
}
Person2 person22 = new Person2 (10, "tian ");
Person2 person23 = new Person2 (10, "tian ");
If (person22.equals (person23 ))
{
System.Out. Println ("Equal ");
}
Else
{
System.Out. Println ("not equal ");
}
If (person22 = person23)
{
System.Out. Println ("Equal ");
}
Else
{
System.Out. Println ("not equal ");
}
}
Public static class Person1
{
Int age;
String name;
Public Person1 (int age, String name)
{
This. age = age;
This. name = name;
}
}
Public static class Person2
{
Int age;
String name;
Public Person2 (int age, String name)
{
This. age = age;
This. name = name;
}
@ Override
Public boolean equals (Object object)
{
If (object = null)
{
Return false;
}
If (this = object)
{
Return true;
}
If (this. getClass ()! = Object. getClass ())
{
Return false;
}
Person2 person2 = (Person2) object;
Return person2.name. equals (name) & person2.age = age;
}
}
}
In the Object class, the hashCode () method is a local method and returns the address value of the Object. The equals () method in the Object class compares the address values of the two objects, therefore, equals () is equal, indicating that the address values of the two objects are equal, so hashCode () is equal.
In common functions, both equals () and hashCode () functions are overwritten. Generally, hashCode () functions need to be overwritten when rewriting equals () functions, otherwise, equals () is equal and hashCode () is not equal. In a collection class, HashSet, HashMap, and Hashtable have the same hashCode () but not equal to equals (), because hashCode () and equals () are overwritten.
Copyright Disclaimer: knowledge is shared, and technology is for communication. Leave a blogger's link for reprinting.