How does a HashSet of a set framework ensure the uniqueness of elements?

Source: Internet
Author: User

How does a HashSet of a set framework ensure the uniqueness of elements?
I. HashSet Principle

We need to remove duplicate elements when using the Set. If equals () is compared one by one during storage, the efficiency is low. The hash algorithm improves the de-duplication efficiency and reduces the use of equals () number of methods
When HashSet calls the add () method to store objects, it first calls the hashCode () method of the object to obtain a hash value, and then searches for objects with the same hash value in the set.
Objects with the same hash value are directly stored in the set.
If there are objects with the same hash value, perform equals () Comparison with objects with the same hash value. If the comparison result is false, the objects are saved. If the value is true, the objects are not saved.

2. Store custom class objects into HashSet to repeat

The hashCode () and equals () methods must be overwritten in the class.
HashCode (): The returned values of objects with the same attributes must be the same. The returned values of different attributes should be as different as possible (improving efficiency)
Equals (): returns true if the attributes are the same. If the attributes are different, false is returned. If the values are false, the hashCode () and equals () methods must be overwritten when the deduplication of custom objects is stored, because the equals method is compared according to the object address value)

Iii. ecplise automatically overrides the hashCode () and equals () Methods
1/* 2 * Why 31? Is a prime number. The number is neither big nor small. Is a good calculation. The fifth power of 2 is reduced by 3 */4 @ Override 5 public int hashCode () {6 final int prime = 31; 7 int result = 1; 8 result = prime * result + (name = null )? 0: name. hashCode (); 9 result = prime * result + (sex = null )? 0: sex. hashCode (); 10 return result; 11} 12 13 @ Override14 public boolean equals (Object obj) {15 if (this = obj) // The called object and the passed object are the same object 16 return true; // returns true17 if (obj = null) // The passed object is null18 return false; // return false19 if (getClass ()! = Obj. getClass () // checks whether the two object's bytecode file is 20 return false; // if not, returns false21 Person other = (Person) obj; // perform downward transformation 22 if (name = null) {23 if (other. name! = Null) 24 return false; 25} else if (! Name. equals (other. name) 26 return false; 27 if (sex = null) {28 if (other. sex! = Null) 29 return false; 30} else if (! Sex. equals (other. sex) 31 return false; 32 return true; 33}

 

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.