First, HashSet Overview:
HashSet implements the set interface, which is supported by a hash table (actually a hashmap instance). It does not guarantee the set's iteration order, especially it does not guarantee that the order is constant. This class allows the use of NULL elements.
Second, the realization of HashSet:
For HashSet, it is based on the implementation of HashMap, hashset the underlying use HashMap to save all elements, so hashset implementation is relatively simple, related hashset operations, basically directly call the underlying HASHMAP related methods to complete,
The source code for HashSet is as follows:
1 public class Hashset<e> 2 extends Abstractset<e> 3 implements Set<e>, Cloneable, Java.io.Seria Lizable 4{5 static final long serialversionuid = -5024744406713321676l; 6 7//Bottom use HashMap to save all elements in the hashset. 8 private transient hashmap<e,object> map; 9 10//define a dummy object as the value of HashMap and define this object as static final. One private static final object PRESENT = new Object (); 12 13/** 14 * The default parameterless constructor constructs an empty hashset. 15 * 16 * The actual underlying will initialize an empty HashMap and use the default initial capacity of 16 and load factor 0.75. * * * Public HashSet () {map = new hashmap<e,object> (); 20} 21 22/** 23 * Constructs a new set that contains the elements in the specified collection. 24 * 25 * The actual underlying uses the default load factor of 0.75 and is sufficient to contain the initial capacity of all elements in the specified * collection to create a hashmap. * @param c where the elements will be stored in the collection in this set. * * * Public HashSet (collection<? extends e> c) {map = new hashmap<e,object> (Math.max (int) (C.size ()/.75f) + 1, 16)); AddAll (c); 32} 33 34/** 35 * Constructs an empty hashset with the specified initialcapacity and loadfactor. 36 * 37 * The actual bottom layer constructs an empty hashmap with corresponding parameters. * @param initialcapacity initial capacity. Loadfactor * @param load factor. * * * Public HashSet (int initialcapacity, float loadfactor) {map = new hashmap<e,object> (initialc Apacity, Loadfactor); 43} 44 45/** 46 * Constructs an empty hashset with the specified initialcapacity. 47 * 48 * The actual bottom layer constructs an empty hashmap with corresponding parameters and loading factor loadfactor of 0.75. * @param initialcapacity initial capacity. * * * Public HashSet (int initialcapacity) {map = new hashmap<e,object> (initialcapacity); 53} 54 55/** 56 * Constructs a new empty-link hash collection with the specified initialcapacity and loadfactor. 57 * This constructor is for package access, not public, and is actually just a support for linkedhashset. 58 * 59 * The actual underlying will construct an empty Linkedhashmap instance with the specified parameters. * @param initialcapacity initial capacity. Loadfactor * @param load factor. Dummy * @param mark. * * * * HASHSET (int initialcapacity, float loadfactor, Boolean dummy) {map = new Linkedhashmap<e,objec T> (inItialcapacity, Loadfactor); 66} 67 68/** 69 * Returns an iterator that iterates over the elements in this set. The order of the returned elements is not specific. 70 * 71 * The bottom layer actually calls the keyset of the underlying HASHMAP to return all keys. 72 * The elements in the hashset are visible, just stored on the key of the underlying hashmap, and the * value uses a static final object identifier. The iterator * @return iterates over the elements in this set. * * * Public iterator<e> Iterator () {Map.keyset return (). Iterator (); 78} 79 80/** 81 * Returns the number of elements in this set (capacity of Set). 82 * 83 * The lower layer actually calls HashMap's size () method to return the number of entry, and the number of elements in the set is obtained. * @return The number of elements in this set (capacity of Set). */-public int size () {map.size return (); 88} 89 90/** 91 * Returns True if this set contains no elements. 92 * 93 * The underlying actual call to HashMap's IsEmpty () determines whether the hashset is empty. 94 * @return Returns TRUE if this set contains no elements. IsEmpty () {$ return map.isempty (); 98} 99 100/** 101 * Returns True if this set contains the specified element. 102 * More specifically, returns True when and only if this set contains an e element that satisfies (O==null e==null:o.equals (E)) 103 *. 104 * 105 * Lower layer actually calls HashMap's conTainskey determines whether the specified key is included. 106 * @param o There are elements in this set that have been tested. 107 * @return Returns TRUE if this set contains the specified element. 108 */109 public boolean contains (Object o) {return map.containskey (o); 111} 112 113/** 114 * If the specified element is not already contained in this set, the specified element is added. 115 * More specifically, if this set does not contain an element E2 that satisfies (E==null. E2==null:e.equals (E2)) 116 *, the specified element e is added to this set. 117 * If this set already contains the element, the call does not change the set and returns false. 118 * 119 * The bottom layer actually puts the element as key into the HashMap. 120 * Due to the HashMap put () method add Key-value pair, when the new put in HashMap entry key 121 * is the same as the set has entry (Hashcode () The return value is equal, by equals comparison also return TR UE), 122 * The value of the newly added entry will overwrite the value of the original entry, but there will be no change to key, 123 * Therefore if an existing element is added to HashSet, the newly added collection element will not be placed in HashMap, 1 24 * There is no change in the original element, which satisfies the feature that the element in set does not repeat. The * @param e is added to the element in this set. 126 * @return Returns TRUE if this set does not already contain the specified element. 127 */N-Public boolean add (e e) {129 return Map.put (E, PRESENT) ==null; 130} 131 132/** 133 * If the specified element exists in this set, it is removed. 134 * More specifically, if this set contains a satisfying (o==null? e==null:o.equThe element e, 135 * of the ALS (e)) is removed. Returns True 136 * If this set already contains the element (or True if this set changes because of a call). (Once the call returns, the set no longer contains the element). 137 * 138 * The lower layer actually calls HashMap's Remove method to delete the specified entry. 139 * @param o an object that needs to be removed if it exists in this set. $ * @return Returns TRUE if the set contains the specified element. 141 */142 public boolean remove (Object o) {143 return Map.Remove (o) ==present; 144} 145 146/** 147 * Remove all elements from this set. When this call returns, the set will be empty. 148 * 149 * The bottom layer actually calls HashMap's clear method to empty all elements in the entry. * * * 151 public void Clear () {map.clear (); 153} 154 155/** 156 * Returns a shallow copy of this HashSet instance: The elements themselves are not copied. 157 * 158 * The underlying actually calls HashMap's Clone () method, gets the shallow copy of the HashMap, and sets it to HashSet. 159 * * * public Object Clone () {161 try {162 hashset<e> Newset = (hashset<e>) Super.clone (); 163 Newset.map = (hashmap<e, object>) Map.clone (); 164 return Newset; 165} catch (Clonenotsupportedexception e) {166 throw new internalerror (); 167 } 168}}
Third, the relevant note:
For objects saved in HashSet, be aware that their equals and hashcode methods are correctly overridden to guarantee the uniqueness of the objects that are placed.
Java Collection: Source code Analysis for HashSet