The set-collection interface in Java implements the principle of non-repetition of Insert objects

Source: Internet
Author: User
Tags repetition

In the Java collection, the rule that determines whether two objects are equal is:

1), determine whether the hashcode of two objects are equal.
If it is not equal, the two objects are not equal, complete
If equal, transfer to 2)
(This is only to improve the storage efficiency requirements, in fact, it is not possible, but if not, the actual use of efficiency will be greatly reduced, so we will do it as necessary.) The question will be highlighted later. )
2), determine whether two objects are equal to the equals operation.
If not equal, neither object is considered equal
If equal, two objects are considered equal (equals () is the key to determine whether two objects are equal)

For objects of a generic class (other than encapsulated type objects such as String):

If the normal class does not override the Hashcode () and Equals () methods, then its object is compared, the hashcode () method in the inherited object class, and the Hashcode () method in the object class is a local method that compares the return value of the method. Compare the address of the object (the reference address), use the new method to create the same content object, two times the generated is of course different objects. Unless you override the Hashcode () method. The Equals () method, as defined in the object class, is also a comparison of object addresses. Summary: If you do not rewrite the normal class hashcode () and the Equals () method, the object reference address is different in the set collection, and the object is not duplicated.

For objects such as String (String, Integer, double etc.):

Because these wrapper classes themselves have overridden the hashcode () method, the returned value of the overridden method is related to the object's content, not to the reference address. The Equals () method in these encapsulation classes is also overridden to compare the object's contents, not the reference address. A word of summary: objects such as String are compared to their contents in the collection, and the same content overwrites existing objects.

The set-collection interface in Java implements the principle of non-repetition of Insert objects

Related Article

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.