dsa

Java TreeSet

In this tutorial, we will learn about the Java TreeSet class and its various operations and methods with the help of examples.

The TreeSet class of the Java collections framework provides the functionality of a tree data structure.

TreeSet class which is implemented in the collections framework an implementation of the SortedSet Interface and SortedSet extends Set Interface. It behaves like simple set with the exception that it stores elements in sorted format. TreeSet uses tree data structure for storage. Objects are stored in sorted, ascending order. But we can iterate in descending order using method

Java TreeSet

Example:

// Java program to demonstrate TreeSet 
import java.util.*; 
  
class TreeSetExample { 
  
    public static void main(String[] args) 
    { 
        TreeSet<String> ts1 = new TreeSet<String>(); 
  
        // Elements are added using add() method 
        ts1.add("A"); 
        ts1.add("B"); 
        ts1.add("C"); 
  
        // Duplicates will not get insert 
        ts1.add("C"); 
  
        // Elements get stored in default natural 
        // Sorting Order(Ascending) 
        System.out.println(ts1); 
    } 
} 
output:
[A, B, C]

Methods in Java Tree Set

Method Description
add(Object o) This method will add the specified element according to the same sorting order mentioned during the creation of the TreeSet. Duplicate entires will not get added.
addAll(Collection c) This method will add all elements of specified Collection to the set. Elements in the Collection should be homogeneous otherwise ClassCastException will be thrown. Duplicate Entries of Collection will not be added to TreeSet.
ceiling?(E e) This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.
clear() This method will remove all the elements.
clone() The method is used to return a shallow copy of the set, which is just a simple copied set.
Comparator comparator() This method will return Comparator used to sort elements in TreeSet or it will return null if default natural sorting order is used.
contains(Object o) This method will return true if given element is present in TreeSet else it will return false.
descendingIterator?() This method returns an iterator over the elements in this set in the descending order.
descendingSet?() This method returns a reverse order view of the elements contained in this set.
first() This method will return first element in TreeSet if TreeSet is not null else it will throw NoSuchElementException.
floor?(E e) This method returns the greatest element in this set less than or equal to the given element, or null if there is no such element.
headSet(Object toElement) This method will return elements of TreeSet which are less than the specified element.
higher?(E e) This method returns the least element in this set strictly greater than the given element, or null if there is no such element.
isEmpty() This method is used to return true if this set contains no elements or is empty and false for the opposite case.
Iterator iterator() Returns an iterator for iterating over the elements of the set.
last() This method will return last element in TreeSet if TreeSet is not null else it will throw NoSuchElementException.
lower?(E e) This method returns the greatest element in this set strictly less than the given element, or null if there is no such element.
pollFirst?() This method retrieves and removes the first (lowest) element, or returns null if this set is empty.
pollLast?() This method retrieves and removes the last (highest) element, or returns null if this set is empty.
remove(Object o) This method is used to return a specific element from the set.
size() This method is used to return the size of the set or the number of elements present in the set.
spliterator?() This method creates a late-binding and fail-fast Spliterator over the elements in this set.
subSet(Object fromElement, Object toElement) This method will return elements ranging from fromElement to toElement. fromElement is inclusive and toElement is exclusive.
tailSet(Object fromElement) This method will return elements of TreeSet which are greater than or equal to the specified element.