Introduction: TreeMap and TreeSet are two important members of the Java Collection Framework, where TreeMap is a common implementation class for the Map interface, and TreeSet is a common implementation class for Set interfaces. Although the interface specification implemented by HASHMAP and HashSet is different, the TreeSet is implemented through TREEMAP, so they are implemented exactly the same way. and the implementation of TREEMAP is the red-black tree algorithm.
TREEMAP is the implementation of the red-black tree data structure, also said to be a self-balanced sort of binary tree, so as to ensure that when the need to quickly retrieve the specified node.
The relationship between TreeSet and TreeMap
To get a sense of the relationship between TreeMap and TreeSet, let's look at some of the source code for the TreeSet class:
public class Treeset<e> extends abstractset<e>
Implements Navigableset<e>, Cloneable, java.io.Serializable
{
Use the Navigablemap key to save the elements of the set collection
private transient navigablemap<e,object> m;
Use a PRESENT as all of the value of the Map collection.
private static final Object PRESENT = new Object ();
The constructor of the package access permission to create the set collection with the specified Navigablemap object
TreeSet (navigablemap<e,object> m)
{
THIS.M = m;
}
Public TreeSet ()//①
{
Create a new TreeMap in a natural sort way,
Create a TreeSet based on the TreeSet,
Use the TREEMAP key to save the elements of the set collection
This (new treemap<e,object> ());
}
Public TreeSet (COMPARATOR< super e> Comparator)//②
{
Create a new TreeMap in a custom sort way.
Create a TreeSet based on the TreeSet,
Use the TREEMAP key to save the elements of the set collection
This (new treemap<e,object> (comparator));
}
Public TreeSet (COLLECTION< extends e> c)
{
Call the ① constructor to create a TreeSet with TreeMap to save the collection elements
This ();
Add all the elements in Collection set C to TreeSet
AddAll (c);
}
Public TreeSet (sortedset<e> s)
{
Call the ② constructor to create a TreeSet with TreeMap to save the collection elements
This (S.comparator ());
Add all elements in SortedSet set S to TreeSet
AddAll (s);
}
TreeSet's other methods are simply calling the TreeMap method to provide the implementation
...
public boolean addall (collection<? extends e> c)
{
if (m.size () = = 0 && c.size () > 0 &&
C instanceof SortedSet &&
M instanceof TreeMap)
{
Cast the C set to the SortedSet collection
sortedset<? Extends e> set = (sortedset<? extends e>) C;
Cast m set to TreeMap set
treemap<e,object> map = (treemap<e, object>) m;
comparator<? Super e> cc = (comparator< super e>) Set.comparator ();
comparator<? Super E> MC = Map.comparator ();
If CC and MC two Comparator equal
if (cc = MC | | (CC!= NULL && cc.equals (MC))
{
To add all the elements in the Collection to the key of the TREEMAP set
Map.addallfortreeset (set, PRESENT);
return true;
}
}
Call the AddAll () method of the parent class directly to implement the
return Super.addall (c);
}
...
}