What is a TreeSet in Java ?
Points To Remember
- TreeSet is ordered and sorted Set.
- TreeSet extends AbstractSet and implements NavigableSet.
- Objects are stored in ascending order based on natural ordering.
- You can even define your sorting rules for sorting a TreeSet using constructors with Comparable or Comparator.
- It is fast for Accessing/Retrieving objects but slow for Adding/Removing objects.
Constructor : TreeSet
TreeSet(); // Default Constructor TreeSet(Collection<? extends E> c); //TreeSet from Collection C TreeSet(Comparator<? super E> comp); // TreeSet with custom ordering as per Comparator TreeSet(SortedSet<E>ss); //TreeSet that contains the elements of ss.
Example : TreeSet Implementation
Suppose we have a Set of Tourists from different countries and we want to sort them according to their names.(Right now we can just neglect the country of the tourists).import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { TreeSet<String> tourists = new TreeSet<String>(); tourists.add("Sachin"); tourists.add("Zahir"); tourists.add("Ravi"); tourists.add("Ishant"); tourists.add("Satty"); tourists.add("Kavita"); tourists.add("Ashish"); tourists.add("Sanket"); tourists.add("Sachin"); // This is duplicate element so will not be added again System.out.println("Original Set:" + tourists); System.out.println("First Name: "+ tourists.first()); System.out.println("Last Name: "+ tourists.last()); } }
Original Set:[Ashish, Ishant, Kavita, Ravi, Sachin, Sanket, Satty, Zahir] First Name: Ashish Last Name: Zahir
Example : TreeSet Implementation Comparator Interface.
Now If we consider the ages of the tourists as well, the above example will fail Since we do not know how to sort the given objects of the class. So we need to define custom sorting logic using Comparator or Comparable. The following example shows how we can use Comparator to sort the Tourists based on their names and if their names are same then based on their ages.
Class : Tourists.java
public class Tourists { private String name; private int age; Tourists(String cricketerName, int age){ this.name = cricketerName; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
Class : SortTouristsByComparator.java
import java.util.Comparator; class SortTouristsByComparator implements Comparator<Tourists> { @Override public int compare(Tourists t1, Tourists t2) { if( t1.getName().compareTo(t2.getName()) != 0) return t1.getName().compareTo(t2.getName()); return t1.getAge() - t2.getAge() ; } }
Class : TreeSetDemo.java
import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { //We pass the object of the Class that implements Comparable //interface for sorting Tourists objects. TreeSet<Tourists> tourists = new TreeSet<Tourists>(new SortTouristsByComparator()); tourists.add(new Tourists("Sachin",35)); tourists.add(new Tourists("Zahir",29)); tourists.add(new Tourists("Ravi",31)); tourists.add(new Tourists("Ishant",19)); tourists.add(new Tourists("Satty",25)); tourists.add(new Tourists("Kavita",12)); tourists.add(new Tourists("Ashish",64)); tourists.add(new Tourists("Sanket",30)); tourists.add(new Tourists("Sachin",23)); for(Tourists tourist : tourists){ System.out.println("name="+tourist.getName() + " age="+tourist.getAge()); } } }
name=Ashish age=64 name=Ishant age=19 name=Kavita age=12 name=Ravi age=31 name=Sachin age=23 name=Sachin age=35 name=Sanket age=30 name=Satty age=25 name=Zahir age=29
Example : TreeSet Implementation using Comparable Interface.
The following example shows how we can use Comparable to sort the Tourists based on their names and if their names are same then based on their ages.
Class : Tourists.java
public class Tourists implements Comparable<Tourists>{ private String name; private int age; Tourists(String cricketerName, int age){ this.name = cricketerName; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public int compareTo(Tourists t){ if(this.getName().compareTo(t.getName()) !=0) return this.getName().compareTo(t.getName()); return this.getAge() - t.getAge(); } }
Class : TreeSetDemo.java
import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { TreeSet<Tourists> tourists = new TreeSet<Tourists>(); tourists.add(new Tourists("Sachin",35)); tourists.add(new Tourists("Zahir",29)); tourists.add(new Tourists("Ravi",31)); tourists.add(new Tourists("Ishant",19)); tourists.add(new Tourists("Satty",25)); tourists.add(new Tourists("Kavita",12)); tourists.add(new Tourists("Ashish",64)); tourists.add(new Tourists("Sanket",30)); tourists.add(new Tourists("Sachin",23)); for(Tourists tourist : tourists){ System.out.println("name="+tourist.getName() + " age="+tourist.getAge()); } } }
name=Ashish age=64 name=Ishant age=19 name=Kavita age=12 name=Ravi age=31 name=Sachin age=23 name=Sachin age=35 name=Sanket age=30 name=Satty age=25 name=Zahir age=29
No comments: