HashSet, hashsetjava

Source: Internet
Author: User

HashSet, hashsetjava
HashSet

HashSet implements the Set interface. It does not guarantee the set iteration sequence; in particular, it does not ensure that the sequence remains unchanged. This class allows the use of null elements.

Implementation

Based on HashMap, HashSet uses HashMap to store all elements at the underlying layer. Therefore, the implementation of HashSet is relatively simple. The operations related to HashSet are basically completed by directly calling the related methods of the underlying HashMap.

Code
public class HashSet<E>      extends AbstractSet<E>      implements Set<E>, Cloneable, java.io.Serializable

Inherits AbstractSet to implement Set, clone, and serialization.

Constructor
/*** The default parameter-free 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> ();}
/*** Construct a new set containing the 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 );}
Iteration, iterator
/*** 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 ();}
Add
// Define a virtual Object as the value of HashMap and define this Object as static final. Private static final Object PRESENT = new Object ();
/*** If the 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 places 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 ;}
Delete & clear
/*** 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 ();}
Others
/*** Returns a superficial copy of The 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 ();}}
/*** Returns 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 ();}
/*** If this set does not contain any elements, true is returned. ** 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 ();}
I am the dividing line of tiantiao

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.