The
HashSet implements the set interface and is supported by a hash table. It does not guarantee the iteration order of the set, especially it does not guarantee that the order is immutable.
public class Hashset<e> extends abstractset<e> implements Set<e>, Cloneable, J
ava.io.Serializable {static final long serialversionuid = -5024744406713321676l;
The bottom layer uses HASHMAP to hold all the elements in the hashset.
private transient hashmap<e,object> map;
Defines a virtual object as the value of HashMap, which defines this object as static final.
private static final Object PRESENT = new Object ();
/** * The default parameterless constructor constructs an empty hashset.
* * The actual bottom 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> ();
/** * Constructs a new set containing the elements in the specified collection.
* * The actual bottom is to create a hashmap using the default load factor 0.75 and the initial capacity sufficient to contain all elements in the specified * collection.
* @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);
/** * Constructs an empty hashset with the specified initialcapacity and loadfactor.
* * The actual bottom layer constructs an empty hashmap with the corresponding parameters.
* @param initialcapacity initial capacity.
* @param loadfactor loading factor. */public HashSet (int initialcapacity, float loadfactor) {map = new hashmap<e,object> (initialc
Apacity, Loadfactor);
/** * Constructs an empty hashset with the specified initialcapacity.
* * The actual bottom layer constructs an empty hashmap with the corresponding parameter and the load factor loadfactor to 0.75.
* @param initialcapacity initial capacity.
* * Public hashset (int initialcapacity) {map = new hashmap<e,object> (initialcapacity);
/** * Constructs a new set of empty link hashes with the specified initialcapacity and loadfactor. * This constructor is for package access, not for public, but actually for LinkeDhashset's support.
* * The actual bottom layer is implemented with an empty Linkedhashmap instance with the specified parameters.
* @param initialcapacity initial capacity.
* @param loadfactor loading factor.
* @param dummy mark. * * HashSet (int initialcapacity, float loadfactor, Boolean dummy) {map = new Linkedhashmap<e,obje
Ct> (initialcapacity, loadfactor); /** * Returns an iterator that iterates through the elements in this set.
The order of the returned elements is not specific.
* * The bottom layer actually calls the keyset of the underlying hashmap to return all key.
* Visible elements in HashSet, only stored in the underlying HashMap key, * value uses a static final object identifier.
* @return The iterator that iterates through 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 size () method of the underlying actual call to HashMap returns 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 this set does not contain any elements.
* * The underlying isempty () that actually invokes HashMap determines whether the hashset is empty.
* @return Returns True if this set does not contain any elements.
* * Public Boolean IsEmpty () {return map.isempty ();
/** * Returns True if this set contains the specified element.
* More specifically, returns True if and only if this set contains an e element that satisfies (O==null e==null:o.equals (e)) *.
* * The bottom of the actual call HashMap ContainsKey to determine whether to include the specified key.
* @param o The elements in this set have been tested.
* @return Returns True if this set contains the specified element.
*/Public Boolean contains (Object o) {return map.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)) *, the specified element e is added to this set.
* If this set already contains the element, the call does not change the set and returns false.
* * The bottom actually puts the element as key into the HashMap. * Since the HashMap put () method adds a Key-value pair, the key * in the entry of the newly placed HashMap is the same as the key in the set with entry (Hashcode () returns the same value, which is also returned through the equals comparison True), * The value of the newly added entry will overwrite the value of the original entry, but the key will not change, * so if an existing element is added to HashSet, the newly added collection element will not be placed has
In Hmap, the original element will not change any more, which satisfies the feature of the element in the set that is not duplicated.
* @param e will be added to the elements in this set.
* @return Returns True if this set does not already 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)), it is removed. Returns True * If this set already contains this element (or: Returns True if this set is changed because of a call).
(Once the call returns, this set no longer contains the element).
* * The underlying remove method that actually invokes HashMap deletes 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;
}
/** * Removes all elements from this set.
When this call returns, the set will be empty.
* * The underlying clear method that actually calls HashMap clears all elements of the entry.
*/public void Clear () {map.clear ();
/** * Returns a shallow copy of this HashSet instance: The elements themselves are not replicated.
* The bottom layer actually invokes the HashMap clone () method, gets the hashmap copy of the shallow table, and sets 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 (); }
}
}
This article is from the "Zhao Yuqiang blog" blog, please be sure to keep this source http://zhaoyuqiang.blog.51cto.com/6328846/1111665