< Prev
Next >


TreeSet is a collection class that extends AbstractSet class implements NavigableSet interface. By default, TreeSet stores its element in an ascending order and retrieval speed of an element out of a TreeSet is quite fast even for a TreeSet with a large number of elements, hence, TreeSet is the best candidate for a situation where one needs to store a large number of sorted elements and their fast access.

Important features of TreeSet.

Constructors :

1) LinkedList()
This constructor creates an empty TreeSet to hold objects of same type.
Example -
TreeSet<String> ts= new TreeSet<String>();

This constructor example creates an TreeSet to hold String objects.

2) TreeSet(Collection <? extends E>c)
This constructor creates a TreeSet initialized with the elements of collection c, with a condition, that collection c holds similar type of objects as the type declared by the TreeSet.
TreeSet<String> ts1 = new TreeSet<String>():
TreeSet<String> ts2 = new TreeSet<String>(ts1);

This constructor example creates an empty TreeSet which is sorted according to the specified Comparator(Comp) in constructor. For a detailed explanation, please refer - Comparator for ordering.

Some important methods of TreeSet class.

TreeSet class inherits methods from NavigableSet interface, which in turn extends Set interface(which extends Collection interface). So, you may see here some new methods from NavigableSet interface..
Methods Description
boolean add(E e) Adds the element e to the TreeSet.
int size() Returns the total number of elements in an LinkedHashSet.
E first() Returns the first element in the TreeSet.
E last() Returns the last element in the TreeSet.
E ceiling(E obj) Searches & returns the smallest element e in TreeSet, such that, e>=obj
E floor(E obj) Searches & returns the greatest element e in TreeSet such that, e<=obj
E higher(E obj) Searches and returns an element e in TreeSet, such that e>obj
E lower(E obj) Searches and returns any element e in TreeSet such that e
boolean isEmpty() Returns true is HashSet is empty, else false.
contains(Object o) Checks if LinkedHashSet contains a specific Object.
boolean remove(Object o) Removes the first occurrence of an Object from the TreeSet, if it is present.
Object[] toArray() Returns an Object array containing all the elements of an TreeSet.
Iterator iterator() Returns an Iterator to iterate over elements of an TreeSet.


Elements of TreeSet in an ascending order, by default.

In this program we have created a TreeSet to hold <Integer> objects and we will find values based on the closest match to certain values in this TreeSet, using methods it has inherited from NavigableSet interface.
import java.util.*;

class TreeSet1
public static void main(String... ar)
TreeSet<Integer> tree= new TreeSet<Integer>();


System.out.println("Content of TreeSet after adding objects" + tree);
System.out.println("Size of TreeSet = "+ tree.size());

System.out.println("Smallest number >=4 in TreeSet : "+ tree.ceiling(4));
System.out.println("Greatest Number <=4 in TreeSet : "+tree.floor(4));

System.out.println("Number higher than 2 in TreeSet = "+tree.higher(2));
System.out.println("Number lower than 2 in TreeSet = "+tree.lower(2));

System.out.println("Afer removing element 3, TreeSet = "+ tree);



Output is - :

Content of TreeSet after adding objects[1, 2, 3, 5, 6]
Size of TreeSet = 5
Smallest number >=4 in TreeSet : 5
Greatest Number <=4 in TreeSet : 3
Number higher than 2 in TreeSet = 3
Number lower than 2 in TreeSet = 1
Afer removing element 3, TreeSet = [1, 2, 5, 6]

Program Analysis

Sorting elements of TreeSet in a descending order.

Here in the next example, we are implementing Comparator interface to give a descending order to the String objects in a TreeSet.
import java.util.*;

class Comp implements Comparator

public int compare(String obj1, String obj2) //overriding Comparator's compare() method to give a descending ordering
return(obj2.compareTo(obj1)); //String's compareTo() method compares two String objects.

public static void main(String... ar)
TreeSet<String> tree= new TreeSet<String>(new Comp2()); //passing an object of implementer of Comparator Interface


System.out.println("Content of TreeSet with descending ordering" + tree);
System.out.println("Size of TreeSet = "+ tree.size());

Output is - :

Content of TreeSet with descending ordering[N, M, H, E, A]
Size of TreeSet = 5

Program Analysis

Here, we have called String's compareTo() method which compares two String objects. However, obj2(and not obj1)invokes compareTo(). This will reverse the outcome of comparison between obj1 and obj2 and TreeSet String objects will be sorted in an descending order.

Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter

< Prev
Next >
< LinkedHashSet Class
Comparator for ordering >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition