TreeSet is one of two sorted collections the other is TreeMap In Java - java.util.TreeMap. TreeSet extends AbstractSet and implements the Java NavigableSet Interface . It creates a collection that uses a tree for storage. Objects are stored in sorted, ascending order according to natural order. Optionally, we can change the natural order of a TreeSet by using a Comparable or Comparator interfaces.
Access and retrieval times are quite fast, which makes TreeSet an excellent choice when storing large amounts of sorted information that must be found quickly. TreeSet might not be used when our application has requirement of modification of set in terms of frequent addition of elements.
Below shown is the hierarchy of the TreeSet.
java.lang.Object
java.util.AbstractCollection
java.util.AbstractSet
java.util.TreeSet
Constructors of TreeSet :
Apart from the methods inherited from its parent classes, TreeSet defines the following methods:
TreeSetSample import java.util.*;
class TreeSetSample { public static void main(String arg[]) { TreeSet<Integer> input = new TreeSet<Integer>(); // LINE A input.add(1); input.add(3); input.add(2); input.add(6); input.add(5); input.add(4); System.out.println(input); // LINE B System.out.println("First Element in Set : " + input.first()); // LINE C System.out.println("Last Element in Set : " + input.last()); // LINE D System.out.println("Element higher to 4 : " + input.higher(4)); // LINE E //higher returns the next higher number to the given number System.out.println("Element lower to 3 : " + input.lower(3)); //lower returns the next lower number to the given number } } OUTPUT[1, 2, 3, 4, 5, 6] First Element in Set : 1 Last Element in Set : 6 Element higher to 4 : 5 Element lower to 3 : 2 DESCRIPTIONIn the above program we demonstrated how TreeSet functions at LINE A we have created an empty TreeSet and added elements to it. At LINE B we are displaying the sorted elements of TreeSet . At LINE C we are getting the first element of the Set using first method. At LINE D we are getting last element of the Set using last method. At LINE E we are displaying the next higher number of 4 in the Set using higher method and at LINE F we are displaying the next lower number of 3 in the Set using lower method.
Now lets see a program to sort the objects using a Comparable interface.
TreeSetCustomSortingDemo import java.util.*;
class TreeSetCustomSortingDemo { public static void main(String arg[]) { TreeSet<Student> students = new TreeSet<Student>(); // LINE B students.add(new Student("Sreeram", 10, 'A')); students.add(new Student("Karthik", 12, 'A')); students.add(new Student("Ram", 14, 'B')); students.add(new Student("Yeshwanthi", 5, 'C')); students.add(new Student("Bhavya", 6, 'D')); System.out.println(students); // LINE C TreeSet newStudents = (TreeSet) students.clone(); } }
class Student implements Comparable<Student> { String name; int rollNumber; char section; public Student(String name, int rollNumber, char section) { this.name = name; this.rollNumber = rollNumber; this.section = section; } public String toString() { return name + " " + rollNumber + " " + section; } public int compareTo(Student o) { return rollNumber - o.rollNumber; } } OUTPUT[Yeshwanthi 5 C, Bhavya 6 D, Sreeram 10 A, Karthik 12 A, Ram 14 B] DESCRIPTIONIn the above program we have demonstrated how to change the natural ordering of the TreeSet depending on the requirements. Firstly we have created a Student class which implements Comparable interface with the fields name , rollNumber , section and implemented toString and compareTo method of Comparable interface. At LINE B in the other class we have created a TreeSet and added Student objects. Now Set will consider the compareTo method in the Student class and sorts the objects accordingly.
|