Similarities and differences of TreeMap and TreeSet:
Same point:
- Both TreeMap and TreeSet are ordered collections, meaning that the values they store are well-sequenced.
- TreeMap and TreeSet are non-synchronous collections, so they cannot be shared across multiple threads, but can be synchronized using method Collections.synchroinzedmap ()
- The running speed is slower than the hash set, their internal operation time for elements is O (Logn), while the Hashmap/hashset is O (1).
Different points:
- The main difference is that TreeSet and TreeMap do not implement set and map interfaces
- TreeSet stores only one object, while TreeMap stores two objects key and value (only the Key object is ordered)
- TreeSet cannot have duplicate objects, while treemap can exist in
TreeSet is the implementation class of Navigableset, Navigableset inherits SortedSet interface, SortedSet is the sub-interface of set;
1PublicClass treeset<e>Extends abstractset<e>2Implements Navigableset<e>, Cloneable, java.io.Serializable3{4/**5* The backing map.6*/7PrivateTransient navigablemap<e,object>M89//Dummy value to associate with a Object in the backing Map10PrivateStaticFinal Object PRESENT =NewObject ();1112/**13* Constructs a set backed by the specified navigable map.14*/TreeSet (navigablemap<e,object>m) {16THIS.M =M17}1819/**20* Constructs a new, empty tree set, sorted according to the21st* Natural ordering of its elements. All elements inserted into22* The set must implement the {@linkComparable} interface.23* Furthermore, all such elements must be <i>mutually24* COMPARABLE</I>: {@codeE1.compareto (E2)} must not throw a25* {@codeClassCastException} for any elements {@codeE1} and26* {@codeE2} in the set. If the user attempts to add an element27* to the set which violates this constraint (for example, the user28* Attempts to add a string element to a set whose elements is29 @ Code add} call would throw A30 * { @code 31 */32 public TreeSet () {33 This (new treemap<e,object> "); 34 }35 36}
From the above TreeSet source can be seen, treeset the bottom of the implementation is achieved through TREEMAP, and treemap of the bottom is how to achieve it?
1PublicTreeMap () {2 Comparator =Null;3}45Public TreeMap (COMPARATOR<?Super k>Comparator) {6This.comparator =Comparator7}8..... (Other construction methods not listed)9//Here is a list of put methods to explain in detail10PublicV Put (K key, V value) {Entry<k,v> T =Root12if (t = =Null) {Compare (key, key);//Type (and possibly null) check14Root =New Entry<> (key, value,Null);Size = 1;modcount++;18ReturnNull;19}20IntcmpEntry<k,v>Parent22//Split comparator and comparable pathscomparator<?Super K> CPR =Comparator24if (CPR! =Null) {25Do{Parent =TCMP =Cpr.compare (key, T.key);28if (CMP < 0)t =T.left;30Elseif (CMP > 0)t =T.right;32Else33ReturnT.setvalue (value);34}while (t! =Null);35}36Else{37if (key = =Null)38ThrowNewNullPointerException ();comparable<?Super k> K = (comparable<?Super k>) key;40Do{The parent =TCMP =K.compareto (T.key);43if (CMP < 0)t =T.left;45Elseif (CMP > 0)t =T.right;47Else48ReturnT.setvalue (value);49}while (t! =Null);50}Wuyi entry<k,v> e =new entry<>52 if (CMP < 053 Parent.left = E; else55 parent.right = E; 56 Fixafterinsertion (e); Size++; Modcount++; return null;60}
From the above TreeMap two construction methods and the insertion method can be seen when the first insert, return NULL, the insertion value does not return null; otherwise the return value is not null; Here are a few things to note:
1, when creating TreeSet or TreeMap, when the parameter is comparator, the parameter must be the implementation subclass of comparator, and when the parameterless constructor is used, Adding elements to TreeSet or TREEMAP requires special attention that the object being added must be a subclass that implements the comparable interface or else it will be an error (object type cannot is cast to java.lang.Comparable), which is also the reason for the implementation of the TreeMap put method, which is a polymorphic representation of the parent object pointing to the subclass reference;
Super k>) key;
2, because the bottom of treeset and treemap are tree-shaped structure, and each node object is entry object
K key; V value; 3 null; Null;
This is the structure of entry, which is a tree-like structure of a linked list node;
3, TreeSet and treemap the bottom are the tree structure is a binary search tree, and is a red and black balance tree, the implementation method:
Fixafterinsertion (e);
Java Collection Class TreeSet, TreeMap