In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how HashSet/TreeSet uses the hashCode () and equal () methods". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how HashSet/TreeSet uses the hashCode () and equal () methods.
one。 problem
1. How HashSet,TreeSet uses the hashCode () and equal () methods
2. When and why should objects in TreeMap,TreeSet implement the Comparable interface?
two。 Answer:
1. HashSet is implemented through HashMap, TreeSet is implemented through TreeMap, but Set only uses Map's key
2. Both key and Set of Map have a common feature that is the uniqueness of sets. TreeMap has more order.
3. HashCode and equal () are for HashMap, because there is no need to sort, so you only need to focus on location and uniqueness.
A. hashCode is used to calculate the hash value, and the hash value is used to determine the hash table index.
B. A linked list is stored at an index in the hash table, so you have to loop through the equal method to compare each object on the chain.
Can we really locate the Entry corresponding to the key value.
C. When put, if it is not located in the hash table, add an Entry in front of the linked list. If it is located, replace the value in Entry and return the old value.
[@ more@] d. When overriding hashCode () and equal () of key, be careful not to associate them with mutable attributes
Otherwise, when the property changes, the hashCode will change, and the equal will be false, so you can't find it in the Map.
And such an object cannot be released because it cannot be found, so it becomes an invalid reference (equivalent to a memory leak).
4. Because TreeMap needs to be sorted, a Comparator is needed to compare the size of the key. Of course, it is also positioned by Comparator.
A. Comparator can be specified when creating a TreeMap, where Comparator.compare is used for sorting
b. If Comparator is not specified when created, the key.compareTo () method is used, which requires key to implement the Comparable interface.
C. TreeMap is implemented using Tree data structures, so positioning can be done using the compare interface.
At this point, I believe you have a deeper understanding of "how HashSet/TreeSet uses the hashCode () and equal () methods". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.