One, set of HashSet (ext: http://blog.csdn.net/zheng0518/article/details/42197337)
1. HashSet Overview:
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.
2. Implementation of HashSet:
For HashSet, it is based on the implementation of HashMap, hashset the underlying use HashMap to save all elements, so hashset implementation is relatively simple, related hashset operations, basically directly call the underlying HASHMAP related methods to complete, The source code for HashSet is as follows:
Public classHashset<e>extendsAbstractset<e>ImplementsSet<e>, cloneable, java.io.Serializable {Static Final LongSerialversionuid = -5024744406713321676l; //the underlying uses HashMap to hold all the elements in the hashset. Private transientHashmap<e,object>map; //defines a dummy object as the value of HashMap, which defines this object as static final. Private Static FinalObject 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. */ PublicHashSet () {map=NewHashmap<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 is sufficient to contain the initial capacity of all elements in the specified * collection to create a hashmap. * @paramc The elements in this set will be stored in the collection. */ PublicHashSet (collection<?extendsE>c) {map=NewHashmap<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 corresponding parameters. * @paraminitialcapacity initial capacity. * @paramloadfactor load factor. */ PublicHashSet (intInitialcapacity,floatloadfactor) {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. * @paraminitialcapacity initial capacity. */ PublicHashSet (intinitialcapacity) {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. * @paraminitialcapacity initial capacity. * @paramloadfactor load factor. * @paramdummy tag. */HashSet (intInitialcapacity,floatLoadfactor,Booleandummy) {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. * @returnThe iterator that iterates over the elements in this set. */ PublicIterator<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. * @returnThe number of elements in this set (capacity of Set). */ Public intsize () {returnmap.size (); } /*** Returns True if this set contains no elements. * * The underlying actual call to HashMap IsEmpty () determines whether the hashset is empty. * @returnreturns True if this set contains no elements. */ Public BooleanIsEmpty () {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. * @paramo The element that has been tested in this set. * @returnreturns True if this set contains the specified element. */ Public Booleancontains (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. * @parame is added to the element in this set. * @returnreturns True if the set does not already contain the specified element. */ Public BooleanAdd (e e) {returnMap.put (e, PRESENT) = =NULL; } /*** If the specified element is present 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. * @paramo An object that needs to be removed if it exists in this set. * @returnreturns True if the set contains the specified element. */ Public BooleanRemove (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 voidClear () {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. */ PublicObject Clone () {Try{HashSet<E> Newset = (hashset<e>)Super. Clone (); Newset.map= (Hashmap<e, object>) Map.clone (); returnNewset; } Catch(clonenotsupportedexception e) {Throw NewInternalerror (); } } }
Second, set of the Linkedhashset
1. Linkedhashset Overview:
Linkedhashset is a hash table and a link list implementation with a predictable iteration order of the set interface. The difference between this implementation and HashSet is that the latter maintains a double-link list that runs on all items. This list of links defines the order of the iterations, which can be in the order of insertion or in the order of access.
Note that this implementation is not synchronous. If multiple threads access the linked hash set at the same time, and at least one of the threads modifies the set, it must remain externally synchronized.
2. Implementation of Linkedhashset:
For Linkedhashset, it inherits from HashSet and is based on Linkedhashmap.
Linkedhashset Bottom uses Linkedhashmap to save all the elements, it inherits and HashSet, all of its methods operate in the same way as hashset, so the implementation of Linkedhashset is very simple, providing only four construction methods, And by passing an identity parameter, call the constructor of the parent class, the underlying constructs a linkedhashmap to implement, in the related operation and the parent class hashset the same operation, directly call the parent class HashSet method. The source code for Linkedhashset is as follows:
Public classLinkedhashset<e>extendsHashset<e>ImplementsSet<e>, cloneable, java.io.Serializable {Private Static Final LongSerialversionuid = -2851667679971038690l; /*** Constructs a new empty link hash set with the specified initial capacity and load factor. * * The lower layer invokes the constructor of the parent class, constructing a Linkedhashmap instance with the specified initial capacity and load factor. * @paraminitialcapacity initial capacity. * @paramloadfactor load factor. */ PublicLinkedhashset (intInitialcapacity,floatloadfactor) { Super(Initialcapacity, Loadfactor,true); } /*** Constructs a new empty-link hash set with the specified initial capacity and default load factor 0.75. * * The lower layer invokes the constructor of the parent class, constructing a Linkedhashmap instance with a specified initial capacity and a default load factor of 0.75. * @paraminitialcapacity initial capacity. */ PublicLinkedhashset (intinitialcapacity) { Super(Initialcapacity,. 75f,true); } /*** Constructs a new empty-link hash set with default initial capacity of 16 and load factor 0.75. * * The lower layer invokes the constructor of the parent class, constructing a Linkedhashmap instance with a default initial capacity of 16 and a load factor of 0.75. */ PublicLinkedhashset () {Super(. 75f,true); } /*** Constructs a new link hash set that is the same as the element in the specified collection. * * The lower layer invokes the constructor of the parent class, constructing a linkedhashmap instance that is sufficient to contain the initial capacity of all elements in the specified collection * and a load factor of 0.75. * @paramc The elements in this set will be stored in the collection. */ PublicLinkedhashset (collection<?extendsE>c) {Super(Math.max (2*c.size (), one),. 75f,true); AddAll (c); } }
Re-start learning Javase_ collection _set