Java hashcode and equals depth analysis and source code explanation

Source: Internet
Author: User

1. When an object is added to ArrayList, it is actually placed in an array maintained by the underlying ArrayList, and when an object is added to LinkedList, a entry object is actually generated inside LinkedList. The structure of the entry object is:

Entry

{

Entry previous;

Object element;

Entry Next;

}

element of the object type is the element we add to the LinkedList, and then entry constructs the forward and backward references previous, next, and finally joins the resulting entry object into the list. In other words, the entry object is maintained in LinkedList.

2. Characteristics of the Equals method of the object class

A) Reflexivity: X.equals (x) should return True

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

c) transitivity: X.equals (y) is true and Y.equals (z) is true, then X.equals (z) should also be true.

D) Consistency: The first invocation of X.equals (y) is true, then the second, third, and nth invocations of x.equals (Y) should also be true, provided that there is no modification between the comparisons X and Y.

e) returns false for non-null references x,x.equals (NULL).

3. Features of the Hashcode () method for the object class:

A) during one execution of a Java application, for multiple invocations of the Hashcode method of the same object, they should return the same value (provided that the information for that object has not changed).

b) For two objects, if you use the Equals method to return True, the hashcode values of the two objects must be the same.

c) 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 (can be the same, can be different), but if they are different, they can improve the performance of the application.

d) 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).

4. When using HashSet, the Hashcode () method is called to determine if the hash code value of the object already stored in the collection is the same as the hash code value of the added object, 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.

5. If we rewrite the Equals method, then we also rewrite the Hashcode method and vice versa.

6. Map: Map's Keyset () method returns a collection of keys because the key of the map cannot be duplicated, so the return type of the keyset () method is set, and the value of map is repeatable, so values () The return type of the method is collection, which can accommodate duplicate elements.

Java hashcode and equals depth analysis and source code explanation

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.