Ava collection --- source code analysis of HashSet, ava --- source code of hashset
1. HasnSet Overview
Hashset implements the set interface, which is supported by a hash table (actually a HashMap instance. It does not guarantee the set iteration sequence. In particular, it does not guarantee that the order remains unchanged. This class allows the use of Null Elements
I. HasnSet implementation
For a HashSet, it is implemented based on HashMap. The HashSet uses HashMap to store all elements at the underlying layer. Therefore, the implementation of HashSet is simple and related HashSet operations, basically, it is implemented by calling the related methods of HashMap.
The source code of HashSet is as follows:
Public class HashSet <E> extends AbstractSet <E> implements Set <E>, Cloneable, java. io. serializable 4 {static final long serialVersionUID =-5024744406713321676L; // use the HashMap underlying layer to save all elements in the HashSet. Private transient HashMap <E, Object> map; // defines a virtual Object as the value of HashMap, which is defined as static final. Private static final Object PRESENT = new Object ();/*** the default no-argument constructor constructs an empty HashSet. ** An empty HashMap is initialized at the underlying layer, and the default initial capacity is 16 and the load factor is 0.75. */Public HashSet () {map = new HashMap <E, Object> () ;}/ *** constructs a new set containing elements in the specified collection. ** The actual underlying layer uses the default load factor 0.75 and is sufficient to contain the initial capacity of all elements in the specified * collection to create a HashMap. * @ Param c the elements in it 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) ;}/ *** construct an empty HashSet with the specified initialCapacity and loadFactor. ** The actual underlying layer constructs an empty HashMap with corresponding parameters. * @ Param initialCapacity initial capacity. * @ Param loadFactor: load factor. */Public HashSet (int initialCapacity, float loadFactor) {map = new HashMap <E, Object> (initialCapacity, loadFactor );} /*** construct an empty HashSet with the specified initialCapacity. ** At the underlying layer, an empty HashMap is constructed based on the corresponding parameter and loading Factor loadFactor 0.75. * @ Param initialCapacity initial capacity. */Public HashSet (int initialCapacity) {map = new HashMap <E, Object> (initialCapacity );} /*** construct a new empty link hash set with the specified initialCapacity and loadFactor. * This constructor is the package access permission and is not disclosed to the public. It is actually only supported by javashashset. ** At the underlying layer, an empty LinkedHashMap instance is constructed with the specified parameters. * @ Param initialCapacity initial capacity. * @ Param loadFactor: load factor. * @ Param dummy mark. */HashSet (int initialCapacity, float loadFactor, boolean dummy) {map = new LinkedHashMap <E, Object> (initialCapacity, loadFactor );} /*** return the iterator for performing iteration on the elements in this set. The returned element sequence is not specific. ** The underlying layer actually calls the keySet of the underlying HashMap to return all keys. * The elements in the HashSet are only stored on the key of the underlying HashMap. * value uses a static final Object identifier. * @ Return Iterator that iterates on the elements in this set. */Public Iterator <E> iterator () {return map. keySet (). iterator () ;}/ *** return the number of elements in the set (set capacity ). ** The number of entries returned by actually calling the size () method of HashMap at the underlying layer gets the number of elements in the Set. * @ Return the number of elements in this set (set capacity ). */Public int size () {return map. size () ;}/ *** returns true if this set does not contain any element. ** The underlying layer actually calls the isEmpty () of HashMap to determine whether the HashSet is empty. * @ Return if this set does not contain any elements, true is returned. */Public boolean isEmpty () {return map. isEmpty ();}/*** returns true if the set contains the specified element. * More specifically, when and only when this set contains a satisfied (o = null? E = null: o. equals (e) *, returns true. ** The underlying layer actually calls the hashinskey of HashMap to determine whether it contains the specified key. * @ Param o There Is A tested element in this set. * @ Return if this set contains the specified element, true is returned. */Public boolean contains (Object o) {return map. containsKey (o);}/*** if this set does not contain the specified element, add the specified element. * More specifically, if this set does not contain meet (e = null? E2 = null: e. equals (e2) *, add the specified Element e to this set. * If this set already contains this element, the call does not change the set and returns false. ** The underlying layer actually puts this element as a key into HashMap. * When a key-value pair is added to the put () method of HashMap, when the key * in the newly added Entry of HashMap is the same as the key of the original Entry in the Set (hashCode () returns the same value, returns true through equals comparison). * The value of the newly added Entry will overwrite the value of the original Entry, but the key will not change, * If an existing element is added to a HashSet, the newly added collection element will not be placed in the HashMap. * The original element will not change, this satisfies the non-repetition of elements in the Set. * @ Param e: The elements added to this set. * @ Return returns true if the set does not contain the specified element. */Public boolean add (E e) {return map. put (e, PRESENT) = null;}/*** if the specified element exists in this set, remove it. * To be more precise, if this set contains a satisfied (o = null? E = null: o. equals (e) Element e, * remove it. If this set already contains this element, true * is returned (or, if this set is changed due to call, true is returned ). (This set no longer contains this element once it is returned ). ** The underlying layer actually calls the remove Method of HashMap to delete the specified Entry. * @ Param o the object to be removed if it exists in this set. * @ Return if set contains the specified element, true is returned. */Public boolean remove (Object o) {return map. remove (o) = PRESENT;}/*** remove all elements from this set. After this call is returned, the set is empty. ** The bottom layer actually calls the clear method of HashMap to clear all elements in the Entry. */Public void clear () {map. clear () ;}/ *** returns a superficial copy of this HashSet instance: these elements are not copied. ** The underlying layer actually calls the clone () method of HashMap to obtain the superficial copy of HashMap and set it to HashSet. */Public Object clone () {try {HashSet <E> newSet = (HashSet <E>) super. clone (); newSet. map = (HashMap <E, Object>) map. clone (); return newSet;} catch (CloneNotSupportedException e) {throw new InternalError ();}}}
Iii. Description:
For objects stored in a HashSet, note that the equals and hashCode methods are correctly rewritten to ensure the uniqueness of the put objects.