What does HashSet mean?
This article shows you what HashSet refers to. It is concise and easy to understand. It will definitely brighten your eyes. I hope you can gain something through the detailed introduction of this article.
HashSet is a collection that does not allow you to store repeating elements, and its implementation is relatively simple, as long as you understand HashMap,HashSet.
Member variable
First, take a look at the member variables of HashSet:
Private transient HashMap map
/ / Dummy value to associate with an Object in the backing Map
Private static final Object PRESENT = new Object ()
There are only two main variables found:
* map: used to store the final data.
* RESENT: is all value values written to map.
Constructor public HashSet () {
Map = new HashMap ()
}
Public HashSet (int initialCapacity, float loadFactor) {
Map = new HashMap (initialCapacity, loadFactor)
}
The constructor is simple, initializing map with HashMap.
Add public boolean add (E e) {
Return map.put (e, PRESENT) = = null
}
The key is the add () method. You can see that it treats the stored object as the key to the HashMap, and the value is all the same PRESENT. Because the key of HashMap cannot be repeated, every time a duplicate value is written to HashSet, value will be overwritten, but key will not be affected, thus ensuring that only non-repeating elements can be stored in HashSet.
The principle of HashSet is relatively simple, almost all with the help of HashMap. Therefore, the problems that will occur in HashMap can not be avoided by HashSet.
What does HashSet refer to above? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.