Override the Equals () method in Java while overriding the Hashcode () method

Source: Internet
Author: User

The public boolean equals (Object obj) in object objects, and the method returns true for any non-null reference value x and Y, when and only if X and Y refer to the same object;
Note: When this method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the Hashcode method, which declares that the equality object must have an equal hash code. As follows:
(1) when Obj1.equals (OBJ2) is true, obj1.hashcode () = = Obj2.hashcode () must be true
(2) when obj1.hashcode () = = Obj2.hashcode () is False, Obj1.equals (OBJ2) must be false
If you do not override Equals, then the comparison will be whether the object's reference points to the same memory address, which is overridden to compare the value of two objects for equality. In particular, the use of equals to compare eight large packaging objects
(such as int,float, etc.) and the string class (because the class has overridden the Equals and Hashcode methods) object, the default comparison is the value, which is the reference address of the comparison when comparing other custom objects
Hashcode is a fast access for hashing data, such as using the Hashset/hashmap/hashtable class to store data, based on the hashcode value of the stored object to determine whether the same.
So if we rewrite the euqals for an object, it means that as long as the values of the member variables of the object are equal then Euqals equals true, but does not rewrite hashcode, then we are new to the object,
When the original object. Equals (New Object) equals True, the hashcode of the two is not the same, resulting in an inconsistent understanding, such as when storing a hash collection (such as set Class), will store two values of the same object,
Cause confusion, therefore, it is also necessary to rewrite hashcode ()
Summary, the custom class to override the Equals method for equivalent comparison, the custom class to override the CompareTo method for comparison of different object sizes, overriding the Hashcode method in order to compare the data to the Hashset/hashmap/hashtable class

Override the Equals () method in Java while overriding the Hashcode () method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.