ava set -- source code analysis of HashSet

1, Overview of HasnSet

Hashset implements the set interface, which is supported by a hash table (actually a HashMap instance). It does not guarantee the iteration order of set. In particular, it does not guarantee that the order is permanent. This class allows Null elements

1, The implementation of HasnSet

For the HashSet, it is implemented based on the HashMap. The bottom layer of the HashSet uses the HashMap to save all elements. Therefore, the implementation of the HashSet is relatively simple. The operations related to the HashSet are basically implemented by calling the relevant methods of the 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;  
 
   // Bottom use HashMap To preserve HashSet All elements in.  
   private transient HashMap<E,Object> map;  
     
   // Define a virtual Object Object as HashMap Of value,Define this object as static final.   
   private static final Object PRESENT = new Object();  
 
   /** 
    * The default parameterless constructor constructs an empty HashSet. 
    *  
    * The actual underlying initializes an empty HashMap with a default initial capacity of 16 and a load factor of 0.75. 
    */  
   public HashSet() {  
   map = new HashMap<E,Object>();  
   }  
 
   /** 
    * Constructs a new set containing the elements in the specified collection. 
    * 
    * The actual underlying uses the default load factor of 0.75 and enough to contain the specified 
    * collection The initial capacity of all elements in to create a HashMap. 
    * @param c 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);  
   }  
 
   /** 
    * Construct 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 Load factor. 
    */  
   public HashSet(int initialCapacity, float loadFactor) {  
   map = new HashMap<E,Object>(initialCapacity, loadFactor);  
   }  
 
   /** 
    * Constructs an empty HashSet with the specified initialCapacity. 
    * 
    * The actual bottom layer constructs an empty HashMap with the corresponding parameters and load factor loadFactor of 0.75. 
    * @param initialCapacity Initial capacity. 
    */  
   public HashSet(int initialCapacity) {  
   map = new HashMap<E,Object>(initialCapacity);  
   }  
 
   /** 
    * Constructs a new null link hash set with the specified initialCapacity and loadFactor. 
    * This constructor is a package access right, which is not exposed to the public. It is actually only the support for LinkedHashSet. 
    * 
    * The actual underlying layer constructs an empty LinkedHashMap instance 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);  
   }  
 
   /** 
    * Returns the iterator that iterates over the elements in this set. The order in which elements are returned is not specific. 
    *  
    * The underlying actually calls the keySet of the underlying HashMap to return all keys. 
    * It can be seen that the elements in the HashSet are only stored on the key of the underlying HashMap, 
    * value Use an Object object Object ID of static final. 
    * @return Iterator that iterates over the elements in this set. 
    */  
   public Iterator<E> iterator() {  
   return map.keySet().iterator();  
   }  
 
   /** 
    * Returns the number of elements in this set (the capacity of the set). 
    * 
    * The bottom layer actually calls the size() method of HashMap to return the number of Entry, and the number of elements in the Set is obtained. 
    * @return The number of elements in this set (the capacity of the set). 
    */  
   public int size() {  
   return map.size();  
   }  
 
   /** 
    * Returns true if the set does not contain any elements. 
    * 
    * The bottom layer actually calls isEmpty() of HashMap to determine whether the HashSet is empty. 
    * @return Returns true if the set does not contain any elements. 
    */  
   public boolean isEmpty() {  
   return map.isEmpty();  
   }  
 
   /** 
    * Returns true if the set contains the specified element. 
    * More specifically, if and only if this set contains a satisfaction (o = = null? E = = null: o.equals (E)) 
    * Returns true for the e element of. 
    * 
    * The bottom layer actually calls the containsKey of the HashMap to determine whether the specified key is included. 
    * @param o There are elements in this set that have been tested. 
    * @return Returns true if the set contains the specified element. 
    */  
   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 (E = = null? E2 = = null: e.equals (E2)) 
    * Element e2, the specified element e is added to this set. 
    * If the set already contains the element, the call does not change the set and returns false. 
    * 
    * The underlying layer actually places the element as a key in the HashMap. 
    * When the key value pair is added to the put() method of HashMap, the key is added to the Entry of HashMap 
    * It is the same as the key of the original Entry in the collection (hashCode() returns the same value and 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, 
    * Therefore, if you add an existing element to the HashSet, the newly added collection element will not be put into the HashMap, 
    * The original elements will not be changed, which satisfies the feature that the elements in the Set are not repeated. 
    * @param e The element that will be 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, it is removed. 
    * More specifically, if this set contains an element e that satisfies (o = = null? e = = null: o.equals (e)), 
    * Remove it. Returns true if the set already contains the element 
    * (Or: returns true if the set changes due to a call). (once the call returns, the set no longer contains the element.). 
    * 
    * The bottom layer actually calls the remove method of HashMap to delete the specified Entry. 
    * @param o Objects that need to be removed if they exist in this set. 
    * @return Returns true if the set contains the specified element. 
    */  
   public boolean remove(Object o) {  
   return map.remove(o)==PRESENT;  
   }  
 
   /** 
    * Remove all elements from this set. When this call returns, the set will be empty. 
    * 
    * The bottom layer actually calls the clear method of HashMap 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 bottom layer actually calls the clone() method of the HashMap to get the shallow copy of the HashMap and set it to the 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();  
       }  
   }  
}

 

3, Description:

For the objects saved in the HashSet, pay attention to correctly rewrite its equals and hashCode methods to ensure the uniqueness of the objects put in.

Tags: Java

Posted on Fri, 03 Apr 2020 15:55:36 -0700 by badproduce