Java record -56-detailed hashset implementation without repeating elements

Source: Internet
Author: User

Detailed hashset implementation without repeating elements

HashSet the Add Method:

Public boolean Add (E E)

If the specified element is not already contained in this set, the specified element is added. More specifically, if this set does not contain an element E2 that satisfies (E==null. E2==null:e.equals (E2)), the specified element e is added to this set. If this set already contains the element, the call does not change the set and returns false.


The Equals method of object:

public boolean equals (Object obj)

Indicates whether an other object is "equal" to this object.

The Equals method implements an equality relationship on a non-null object reference:

    • Reflexivity: X,x.equals (x) should return true for any non-null reference value.

    • Symmetry: x.equals (y) should return true for any non-null reference value x and Y, if and only if Y.equals (x) returns True.

    • Transitivity: For any non-null reference value x, Y, and Z, if X.equals (y) returns true and Y.equals (z) returns True, then X.equals (z) should return true.

    • Consistency: For any non-null reference value x and Y, multiple calls to X.equals (Y) always return TRUE or always return false, provided that the information used in the Equals comparison on the object has not been modified.

    • X,x.equals (NULL) should return FALSE for any non-null reference value.

The Equals method of object implements the most differentiated equality relationship on the object, that is, for any non-null reference value x and Y, this method returns True if and only if X and Y refer to the same object (x = = Y has a value of true).

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.


Java record -56-detailed hashset implementation without repeating elements

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.