标签:
TreeSet is another popular implementation of Set interface along with HashSet and LinkedHashSet. All these implementations of Set interface are required in different scenarios. If you don’t want any order of elements, then you can use HashSet. If you want insertion order of elements to be maintained, then use LinkedHashSet. If you want elements to be ordered according to some Comparator, then use TreeSet. The common thing of these three implementations is that they don’t allow duplicate elements.
In this article, I have tried to explain two examples of Java TreeSet. One example doesn’t use Comparator and another example uses Comparator to order the elements. You can go through some basic properties of TreeSet class here.
You already know that if you don’t pass any comparator while creating a TreeSet, elements will be placed in their natural ascending order. In this example, we create a TreeSet of Integers without supplying any Comparator like this,
Let’s add some integer elements to it.
Print these elements and observe the output.
System.out.println(set); //Output : [7, 11, 18, 23, 38, 41, 69]
You can notice that elements are placed in the ascending order.
The whole code for this example is,
public class TreeSetExample { public static void main(String[] args) { //Creating a TreeSet without supplying any Comparator TreeSet<Integer> set = new TreeSet<Integer>(); //Adding elements to TreeSet set.add(23); set.add(11); set.add(41); set.add(7); set.add(69); set.add(18); set.add(38); //printing elements of TreeSet System.out.println(set); //Output : [7, 11, 18, 23, 38, 41, 69] } }
理解:默认情况下,元素为Integer的时候,元素是递增顺序的
In this example, we create one TreeSet by supplying a customized Comparator. In this example, we will try to create a TreeSet of Student objects ordered in the descending order of the percentage of marks they have obtained. That means, student with highest marks will be placed at the top.
Let’s create ‘Student’ class with three fields – id, name andperc_Of_Marks_Obtained.
class Student { int id; String name; int perc_Of_Marks_Obtained; public Student(int id, String name, int perc_Of_Marks_Obtained) { this.id = id; this.name = name; this.perc_Of_Marks_Obtained = perc_Of_Marks_Obtained; } @Override public String toString() { return id+" : "+name+" : "+perc_Of_Marks_Obtained; } }
Let’s define our own Comparator class “MyComparator” which compares the marks of two students.
class MyComparator implements Comparator<Student> { @Override public int compare(Student s1, Student s2) { if(s1.id == s2.id) { return 0; } else { return s2.perc_Of_Marks_Obtained - s1.perc_Of_Marks_Obtained; } } }
Important Note : TreeSet doesn’t use hashCode() and equals() methods to compare it’s elements. It uses compare() (or compareTo()) method to determine the equality of two elements. Therefore, I have kept the code which compares two Student objects based on their id in compare method itself. This removes possible duplicate elements (elements having same id) from the TreeSet.
Create one TreeSet of ‘Student‘ objects with ‘MyComparator‘ as a Comparator.
//Instantiating MyComparator MyComparator comparator = new MyComparator(); //Creating TreeSet with ‘MyComparator‘ as Comparator. TreeSet<Student> set = new TreeSet<Student>(comparator);
Add some elements of type ‘Student‘ to this TreeSet.
set.add(new Student(121, "Santosh", 85)); set.add(new Student(231, "Cherry", 71)); set.add(new Student(417, "David", 82)); set.add(new Student(562, "Praveen", 91)); set.add(new Student(231, "Raj", 61)); //Duplicate element set.add(new Student(458, "John", 76)); set.add(new Student(874, "Peter", 83)); set.add(new Student(231, "Hari", 52)); //Duplicate element set.add(new Student(568, "Daniel", 89));
Iterate through the TreeSet.
//Iterating through TreeSet Iterator<Student> it = set.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
Output will be,
562 : Praveen : 91 568 : Daniel : 89 121 : Santosh : 85 874 : Peter : 83 417 : David : 82 458 : John : 76 231 : Cherry : 71
You can notice that student with highest percentage of marks is placed at the top and also duplicate elements are not allowed in the TreeSet.
标签:
原文地址:http://www.cnblogs.com/Guoyutian/p/5185571.html