HashSet source code analysis (JDK1.8)
Article directory
HashSet implementation principle
Most of the methods of HashSet are implemented through its built-in HashMap. When we store an element in HashSet, we actually store a key-value pair in its built-in HashMap. The key is the element we store, and the value is actually a Object, we don't care. Because the put method in HashMap will overwrite the storage of duplicate keys by default, so duplicate elements will not be stored in HashSet. Therefore, as long as you understand the principle of HashMap, you also know the principle of HashSet.
We discussed the source code of HashMap in detail in the previous blog, Portal: The most comprehensive HashMap source code analysis (JDK1.8)
Properties of HashSet
HashSet method
Construction method
Empty parameter construction HashSet()
In fact, it is to create an instance for the built-in HashMap through the empty parameter construction of HashMap
HashSet(Collection<? extends E> c)
If a Collection c is passed, all the elements in c will be added to the newly created HashSet. Here, HashMap(int initialCapacity) is called, and the initial size of HashMap will be set to be enough to put all the elements in c, the minimum For 16
, the addAll method in the AbstractSet inherited by HashSet will be called here. The source code of this method is as follows:
In fact, it is to call the add method rewritten in HashSet for each element in c, and store each element
HashSet(int initialCapacity, float loadFactor)
In fact, it is to call HashMap(int initialCapacity, float loadFactor) to set the initial capacity and load factor of the built-in HashMap
HashSet(int initialCapacity)
In fact, it is to call HashMap(int initialCapacity) to set the initial capacity of the built-in HashMap, using the default load factor of 0.75
HashSet(int initialCapacity, float loadFactor, boolean dummy)
The value of this dummy parameter of boolean type is actually not important. One more parameter is to distinguish it from other constructors. Calling this constructor with 3 parameters will create LinkedHashMap as the built-in HashMap. LinkedHashMap inherits HashMap, and LinkedHashMap saves The insertion order of records, when traversing LinkedHashMap with Iterator, the first record must be inserted first.
CRUD method
add(E e)
add(E e) can either add (Create) or change (Update). In fact, it calls the put method of the built-in HashMap. The key is the element we want to store, and the value is PRESENT. The put method of HashMap returns null to indicate that the addition is successful
remove(Object o)
In fact, the remove method of the built-in HashMap is called. If the remove method of HashMap is successfully deleted, it will return value, otherwise it will return null
Other methods
size()
Calling the size() method of the built-in HashMap will return the number of key-value pairs in the map, that is, the number of elements in the HashSet
iterator()
Returns the key iterator of the built-in HashMap, which is the element iterator of the HashSet
contains(Object o)
Call the containsKey method of the built-in HashMap, which in turn calls the getNode method to find the corresponding node