Contains method of HashSet

Source: Internet
Author: User
HashSet discovery: 1: HashSet is a Set, which is also called a Set. The concept of a set is that elements are unordered and unique in the set. Disorder is meaningless to computers, because computers always store objects in a certain order. How is it implemented? The name is already clear. HashSet is to identify whether an object is unique through the hashconde of the element (object. Therefore, the HashSet object contains a list of hashcode of objects in a collection, and each time the set is executed. when adding (obj), The hashcode of obj will be taken out for comparison with its internal list. If it is not equal, add it to the set, at the same time, add the hashcode of obj to the list. If there is an equal value, call the equals method of obj to compare it with each object. If there is no equal value, add it to the set. If there are still equal, the add operation will not be executed. Public Set add (Object obj ){
Int code = obj. hashCode ();
If (code not in this. hashcodeList) {// if there is no equivalent hashcode. Add obj
This. valueList. add (obj );
This. hashcodeList. add (code );
Return true;
} Else {// otherwise, check whether there is equals equal to it.
If (code in this. valueList) {// Yes, add failed
Return false;
} Else {// No, add successful
This. valueList. add (obj );
Return true;
}
}
} In fact, it is equivalent to executing the contains method once. It determines whether to add the contains Method Based on the return value of the contains method. That is to say, the criteria for determining that a HashSet already contains an object is: Their hashcode is equal or their equals method returns true. After being depressed for a long time, how can I compare hashcode first? Eclipse will give a warning on why toString () is overwritten and hashcode () is not overwritten. Both of them are related to the equality of objects. The hashcode of the Object is calculated based on the memory address of the Object, so that it is not repeated. The hashcode of the String is different. As long as the content of the String is equal (equals returns true), their hashcode is equal. Note that the HashSet is cached. After the first contains command is executed, all hashcodes are cached, And the cached hashcode (that is, the list) is used when the contains command is called for comparison ), therefore, it is recommended that HashSet be used only to store immutable objects. Otherwise, the returned value of the contains method is inaccurate. The following principle should be noted: 1: For immutable classes (String, Integer, and self-defined immutable classes), make sure that when equals returns true, their hashcode values are equal. 2: For immutable classes, try to satisfy 1. To avoid problems with the Set comparison mentioned above. 3: When overwriting the equals method of a class, you must note that the value of a. equals (B) must be equal to the value of B. equals (.

 

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.