In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces how to analyze the principle of HashSet implementation from the perspective of source code in Java interview. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Interviewer: what are the characteristics of HashSet?
Candidate: HashSet is implemented from the set interface. The elements in the set collection are unordered and cannot be repeated.
Interviewer: so how does HashSet ensure that the elements are not duplicated?
Candidate: because the underlying implementation of HashSet is based on HashMap, when you new a HashSet, you actually new a map, and when you execute the add method, you actually call the put method of map. Value is always PRESENT, so according to a feature of HashMap: when you put a key-value pair into HashMap, first determine the storage location of the Entry based on the hashCode () return value of key. If the hash values of the two key are the same, then their storage locations are the same.
If the equalus comparison of the two key returns true. Then the value of the newly added Entry will overwrite the original Entry and the value,key will not be overwritten. Therefore, if you add an existing element to the HashSet, the newly added collection element will not overwrite the existing collection element
Source code analysis
Let's first take a look at the no-parameter constructor:
Public HashSet () {map = new HashMap ();}
Obviously, when you new a HashSet, you actually new a HashMap.
Take a look at the add method again:
Private static final Object PRESENT = new Object (); public boolean add (E e) {return map.put (e, PRESENT) = = null;}
To define a virtual Object PRESENT is to insert the value corresponding to key-value into map, because only key is needed in HashSet, and HashMap is a key-value key-value pair; therefore, when adding a key-value pair to map, the value of the key-value pair is always PRESENT.
Most of the methods of HashSet in the source code are realized by calling the method of HashMap. For other methods, please check the source code yourself.
Java interview on how to analyze the principle of HashSet implementation from the source point of view to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.