The following content is based on jdk1.7.0_79 source code;
About HashSet, Linkedhashset, TreeSet
The implementation class of the set interface, the most important feature is that duplicate elements are not allowed;
HashSet: Based on HASHMAP implementation, a set with relatively good performance;
Linkedhashset: Based on the LINKEDHASHMAP implementation, a set that holds the insertion order;
TreeSet; Based on the TreeSet implementation, a set that implements the ordering;
Class Diagram relationships
SOURCE Analysis
The implementation class of the set interface is relatively simple, if familiar with HashMap, Linkedhashmap, TreeMap source, HashSet, Linkedhashset, TreeSet code will be well understood, Because it is basically the method to call the corresponding map to achieve;
hashset part Source :
PackageJava.util; Public classHashset<e>extendsAbstractset<e>ImplementsSet<e>, cloneable, java.io.serializable{Static Final LongSerialversionuid = -5024744406713321676l; //HashMap of storage elements Private transientHashmap<e,object>map; //a redundant empty object that holds the value of all key-value pairs in the value,map of the key corresponding to the map is a reference to the same null object Private Static FinalObject PRESENT =NewObject (); /*** Construction method, directly generate a corresponding HashMap*/ PublicHashSet () {map=NewHashmap<>(); } //omit part of the code .....//returns the key iterator for HashMap, the element in HashSet is actually the key element in HashMap PublicIterator<e>iterator () {returnMap.keyset (). iterator (); } //call the Put method of HashMap to put the E-present key value on the object into the map Public BooleanAdd (e e) {returnMap.put (e, PRESENT) = =NULL; } //omit part of the code .....}
Linkedhashmap Source code, such as the following, few codes, mainly the construction method,
Based on the parameters passed in, the hashset (int initialcapacity, float loadfactor, Boolean Dummy) method of the parent class HashSet is called to generate a Linkedhashmap object storage collection element:
PackageJava.util; Public classLinkedhashset<e>extendsHashset<e>ImplementsSet<e>, cloneable, java.io.Serializable {Private Static Final LongSerialversionuid = -2851667679971038690l; PublicLinkedhashset (intInitialcapacity,floatloadfactor) { Super(Initialcapacity, Loadfactor,true); } PublicLinkedhashset (intinitialcapacity) { Super(Initialcapacity,. 75f,true); } PublicLinkedhashset () {Super(. 75f,true); } PublicLinkedhashset (collection<?extendsE>c) {Super(Math.max (2*c.size (), one),. 75f,true); AddAll (c); }}
Construction method for generating Linkedhashmap in hashset hashset (int initialcapacity, float loadfactor, Boolean dummy)
HashSet (intfloatboolean dummy) { new linkedhashmap<>( Initialcapacity, Loadfactor); }
treeset part Source :
There is a Navigablemap type map and an empty object, Navigablemap is assigned a TreeMap object in the constructor method, and present is the same reference to the value object in all key-value pairs;
Public class extends Abstractset<e> implements navigableset<e>, cloneable, java.io.serializable{ /** * TreeMap Object * / privatetransient Navigablemap<e,object> m; // null object, the same reference to the value object in all map key-value pairs Private Static Final New Object ();
Construction method, visible TreeSet is implemented based on TreeMap:
TreeSet (navigablemap<e,object> m) { this. m = m; } Public TreeSet () { this (new treemap<e,object>()); }
Look at other TreeSet methods in the source code, basically by calling TreeMap method to achieve;
// omit part of the code ... public navigableset<e> Descendingset () { returnnew treeset< >(M.descendingmap ()); } Public int size () { return m.size (); } Public Boolean IsEmpty () { return m.isempty (); } // omit part of the code ...
Add a sentence
The key to learning set set is to learn map well.
"Java Collection" HashSet, Linkedhashset, TreeSet