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. For HashSet, it is based on HASHMAP implementation, hashset the underlying use HashMap to save all the elements, so hashset implementation is relatively simple, related hashset operations, basically directly call the underlying HASHMAP related methods to complete.
Public class HashSet<E> extendsAbstractset<e> implements Set<e>, cloneable, java.io.Serializable {staticFinalLong Serialversionuid =-5024744406713321676L//Bottom use HashMap to save all elements in the hashset. Privatetransient hashmap<e,object> map;//Define a dummy object as the value of HashMap and define this object as static final. PrivateStaticFinalObject PRESENT =NewObject ();/** * The default parameterless constructor constructs an empty hashset. * * The actual underlying will initialize an empty HashMap and use the default initial capacity of 16 and load factor 0.75. */Public HashSet () {map =NewHashmap<e,object> (); }/** * Constructs a new set that contains the elements in the specified collection. * * 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<?extendsE> c) {map =NewHashmap<e,object> (Math.max ((int) (C.size ()/. thef) +1, -)); AddAll (c); }/** * Constructs an empty hashset with the specified initialcapacity and loadfactor. * * The actual bottom layer constructs an empty hashmap with corresponding parameters. * @param initialcapacity initial capacity. * @param loadfactor loading factor. */Public HashSet (int initialcapacity, float loadfactor) {map =NewHashmap<e,object> (initialcapacity, loadfactor); }/** * Constructs an empty hashset with the specified initialcapacity. * * The actual bottom layer constructs an empty hashmap with corresponding parameters and load factor loadfactor of 0.75. * @param initialcapacity initial capacity. */Public HashSet (int initialcapacity) {map =NewHashmap<e,object> (initialcapacity); }/** * Constructs a new empty-link hash collection with the specified initialcapacity and loadfactor. * This constructor is for package access, not public, and is actually just a support for linkedhashset. * * The actual underlying will be implemented with an empty Linkedhashmap instance constructed with the specified parameters. * @param initialcapacity initial capacity. * @param loadfactor loading factor. * @param dummy mark. */HashSet (int initialcapacity, float loadfactor, Boolean dummy) {map =NewLinkedhashmap<e,object> (initialcapacity, loadfactor); }/** * Returns an iterator that iterates over the elements in this set. The order of the returned elements is not specific. * * The underlying actual call to the underlying HashMap keyset to return all keys. * The elements in the hashset are visible only on the underlying HashMap key, * value uses a static final object identifier. * The iterator that iterates over the elements in this set @return . */Public iterator<e> Iterator () {returnMap.keyset (). iterator (); }/** * Returns the number of elements in this set (capacity of Set). * * The number of elements in the set is obtained by actually calling HashMap's size () method to return the quantity of entry. * @return The number of elements in this set (capacity of Set). */public int size () {returnMap.size (); }/** * Returns True if this set contains no elements. * * The underlying actual call to HashMap IsEmpty () determines whether the hashset is empty. * @return Returns True if this set contains no elements. */public Boolean isEmpty () {returnMap.isempty (); }/** * Returns True if this set contains the specified element. * To be more exact, returns True when and only if this set contains an e element that satisfies (o==null. E==null:o.equals (e)) *. * * The underlying actual call to HashMap ContainsKey determines whether the specified key is included. * @param o The existence of the element that has been tested in this set. * @return Returns True if this set contains the specified element. */Public Boolean contains (Object o) {returnMap.containskey (o); }/** * If the specified element is not already contained in this set, the specified element is added. * More specifically, if this set does not contain an element E2 that satisfies (E==null. E2==null:e.equals (E2)) *, add the specified element e to this set. * If this set already contains the element, the call does not change the set and returns false. * * The underlying actually puts the element as key into the HashMap. * Because the HashMap put () method adds the Key-value pair, when the new HashMap in the entry of the key * is the same as the entry of the set (Hashcode () The return value is equal, and by equals compares true), * The value of the newly added entry will overwrite the value of the original entry, but the key will not change, so if you add an existing element to hashset, the newly added collection element will not be placed in the HashMap, and the original element does not have any Change, which satisfies the feature that the element in set does not repeat. * @param E is added to the element in this set. * @return Returns True if this set does not already contain the specified element. */Public boolean Add (E e) {returnMap.put (e, PRESENT) = =NULL; }/** * If the specified element exists in this set, it is removed. * Rather, if this set contains an element e that satisfies (o==null. E==null:o.equals (e)), * it is removed. Returns True 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). * * The lower layer actually calls HashMap's Remove method to delete the specified entry. * @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. */public boolean remove (Object o) {returnMap.Remove (o) ==present; }/** * Removes all elements from this set. When this call returns, the set will be empty. * * The lower layer actually calls HashMap's clear method to empty all elements in the entry. */public void Clear () {map.clear (); }/** * Returns a shallow copy of this HashSet instance: The elements themselves are not copied. * * The underlying actually calls HashMap's Clone () method, gets the shallow copy of the HashMap, and sets it to HashSet. */Public Object Clone () {Try{hashset<e> Newset = (hashset<e>)Super. Clone (); Newset.map = (hashmap<e, object>) Map.clone ();returnNewset; }Catch(Clonenotsupportedexception e) {Throw NewInternalerror (); } } }
For objects saved in HashSet, be aware that their equals and hashcode methods are correctly overridden to guarantee the uniqueness of the placed objects
Analysis of Java-Foundation-hashset