On the Equals method and Hashcode method of the object class

Source: Internet
Author: User

For the characteristics of the equals of the object class, for non-null references:

1. Reflexivity: X.equals (x) return true;

2. Symmetry: X.equals (y) is true, then y.equals (x) is also true;

3. Transitivity: X.equals (y) is true,y.equals (z) is true, then X.equals (z) is also true;

4. Consistency: X.equals (y) is the first call to True, then X.equals (y) for the second time, third time, ... , the nth invocation is also true, provided that no x is modified between comparisons and Y is not modified.

5.x.equals (NULL) returns FALSE.

When you override the Equals method, you also need to override the Hashcode method to ensure that the hashcode value of the same object is the same.

Features of the Hashcode method for the object class:

1. During one execution of a Java application, the same value should be returned for multiple calls to the Hashcode method of the same object. (provided that the information of the object has not changed);

2. For two objects, if the Equals method is used to return true, the hashcode values of the two objects must be the same;

3. For two objects, if you use the Equals method to compare the return false, the hashcode values of the two objects do not require a certain difference, that is, they can be the same or different. However, if they are different, you can improve the performance of your app.

4. For the object class, the hashcode values of the different object objects are different (the Hashcode value of the object class represents the address of the objects, and if the other classes override the Hashcode () method, then the hashcode value does not necessarily mean the address. )。

If we rewrite the Equals method, then we have to rewrite the Hashcode method, and vice versa.

How the collection is added:

When HashSet is used, the Hashcode () method is called to determine whether the Hashcode value of the object already stored in the collection is consistent with the Hashcode value of the object being added, and if it is inconsistent, add it directly, and if it is consistent, then compare the Equals method, If the Equals method returns true to indicate that the object has been added, no new objects will be added or added.

string S1 = new String ("a"); String s2 = new String ("a"); System.out.println (S1. Hashcode () = = S2. Hashcode ());//returns Truehashset set = new HashSet (); Set.add (S1); Set.add (S2);

The result is only one increase. Because the string class overrides the Equals and Hashcode methods, the Hashcode method simply converts the contents of the string into an integer value through a certain conversion. Therefore, the Equals method of the string class, as long as two strings have the same content, equals returns True.

Another example:

First, define a people class that does not override the Equals method and the Hashcode method.

    public class people      {          private String name;              Public people (String name)          {              super ();              this.name = name;          }          Omit Get/set Method      }  

People p1 = new People ("a");          People p2 = new People ("a");          Set.add (p1);          

Execution Result:

P1 and P2 are added to the set set, because the people class inherits from the object class, and the value returned by the Hashcode () method of the object class is a representation of the address, because the P1 and the P2 address are not the same, so the hashcode value is not the same, So it will be added to the set set.

On the Equals method and Hashcode method of the object class

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.