Introduction to TreeSet
We can see that TreeSet first understands him from the most basic. He is a class and his position is in the java. util package.
Let's take a look at his inherited architecture diagram:
Interfaces implemented by this class:
1TreeSet case:
TreeSet is an ordered set. The elements in the TreeSet are arranged in ascending order. By default, they are arranged in natural order. This means that the elements in the TreeSet must implement the Comparable interface. Or there is a custom comparator.
When constructing a TreeSet object, we can pass the Comparator object that implements the Comparator interface.
import java.util.Iterator;import java.util.*;public class TreeSetTest { public static void main(String[] args) { Set ts = new TreeSet(); ts.add("abc"); ts.add("xyz"); ts.add("rst"); Iterator it = ts.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
Output:
Abc
Rst
Xyz
The printed results are not in the same order as they were previously added. They are sorted by a letter sort. This is because the String class implements the Comparable interface.
If the objects of a defined class are to be added to the TreeSet, the class must implement the Comparable interface.
Import java. util. iterator; import java. util. *; public class TreeSetTest {public static void main (String [] args) {Set ts = new TreeSet (); ts. add (new Teacher ("zhangsan", 1); ts. add (new Teacher ("lisi", 2); ts. add (new Teacher ("wangmazi", 3); ts. add (new Teacher ("mazi", 3); Iterator it = ts. iterator (); while (it. hasNext () {System. out. println (it. next () ;}} class Teacher implements Comparable {int Num; String name; Teacher (String name, int num) {this. num = num; this. name = name;} public String toString () {return "" + num + "name" + name;} public int compareTo (Object o) {Teacher ss = (Teacher) o; int result = num> ss. num? 1: (num = ss. num? 0:-1); if (result = 0) {result = name. compareTo (ss. name);} return result ;}}
NOTE: If int result = num> ss. num? 1: (num = ss. num? 0:-1); int result = ss. num> num? 1: (ss. num = num? 0:-1); then the result is in reverse order, not in ascending order.
When Arrays is used to sort the elements in the array, a comparator can be passed.
When you use Collections to sort elements in a set, you can pass a comparator.
So when we use TreeSet to sort the elements added to it, can we pass in a comparator?
Public TreeSet (Comparator <? Super E> comparator ){
This (new TreeMap <E, Object> (comparator ));
}
By viewing its constructor, you can pass in a comparator.
Construct a new empty TreeSet, Which is sorted by the specified comparator. All elements inserted into the set must be performed by the specified comparator.Mutual comparison: For any two elements e1 and e2 in the set, execute comparator. compare (e1, e2) and do not throw ClassCastException. If you try to add elements that violate this constraint to the set, the add call will throw ClassCastException.
Import java. util. Comparator;
Import java. util. Iterator;
Import java. util. TreeSet;
Public class TreeSetTest {
Public static void main (String [] args ){
TreeSet ts = new TreeSet (new Teacher. TeacherCompare ());
Ts. add (new Teacher ("zhangsan", 2 ));
Ts. add (new Teacher ("lisi", 1 ));
Ts. add (new Teacher ("wangmazi", 3 ));
Ts. add (new Teacher ("mazi", 3 ));
Iterator it = ts. iterator ();
While (it. hasNext ()){
System. out. println (it. next ());
}
}
}
Class Teacher {
Int num;
String name;
Teacher (String name, int num ){
This. num = num;
This. name = name;
}
Public String toString (){
Return "student ID:" + num + "name:" + name;
}
Static class TeacherCompare implements Comparator {// a Comparator provided by the instructor
Public int compare (Object o1, Object o2 ){
Teacher s1 = (Teacher) o1; // Transformation
Teacher s2 = (Teacher) o2; // Transformation
Int result = s1.num> s2.num? 1: (s1.num = s2.num? 0:-1 );
If (result = 0 ){
Result = s1.name. compareTo (s2.name );
}
Return result;
}
}
}
2. Implementation principle of TreeSet:
In fact, TrreSet is implemented by TrreMap:
At the underlying layer of the TreeSet, A NavigableMap is used to save the elements of the TreeSet set. However, as NavigableMap is only an interface, the underlying layer still uses TreeMap to include all elements in the Set.
As for a deeper exploration can look at this blog: http://mojijs.com/2016/04/215498/index.html