Java TreeSet

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

Creating a TreeSet

In order to create a tree set, we must import the java.util.TreeSet package first.

Once we import the package, here is how we can create a TreeSet in Java.


TreeSet<Integer> numbers = new TreeSet<>();

Methods of TreeSet:

Insert Elements to TreeSet

  • add() - inserts the specified element to the set
  • addAll() - inserts all the elements of the specified collection to the set

For example,

import java.util.TreeSet;

class Main {
    public static void main(String[] args) {

        TreeSet<Integer> evenNumbers = new TreeSet<>();

        // Using the add() method
        evenNumbers.add(2);
        evenNumbers.add(4);
        evenNumbers.add(6);
        System.out.println("TreeSet: " + evenNumbers);

        TreeSet<Integer> numbers = new TreeSet<>();
        numbers.add(1);

        // Using the addAll() method
        numbers.addAll(evenNumbers);
        System.out.println("New TreeSet: " + numbers);
    }
}

Output:

TreeSet: [2, 4, 6]
New TreeSet: [1, 2, 4, 6]

Access TreeSet Elements

To access the elements of a tree set, we can use the iterator() method. In order to use this method, we must import java.util.Iterator package. For example,

import java.util.TreeSet;
import java.util.Iterator;

class Main {
    public static void main(String[] args) {
        TreeSet<Integer> numbers = new TreeSet<>();
        numbers.add(2);
        numbers.add(5);
        numbers.add(6);
        System.out.println("TreeSet: " + numbers);

        // Calling iterator() method
        Iterator<Integer> iterate = numbers.iterator();
        System.out.print("TreeSet using Iterator: ");
        // Accessing elements
        while(iterate.hasNext()) {
            System.out.print(iterate.next());
            System.out.print(", ");
        }
    }
}

Output:

TreeSet: [2, 5, 6]
TreeSet using Iterator: 2, 5, 6,

Remove Elements

  • remove() - removes the specified element from the set
  • removeAll() - removes all the elements from the set

For example,

import java.util.TreeSet;

class Main {
    public static void main(String[] args) {
        TreeSet<Integer> numbers = new TreeSet<>();
        numbers.add(2);
        numbers.add(5);
        numbers.add(6);
        System.out.println("TreeSet: " + numbers);

        // Using the remove() method
        boolean value1 = numbers.remove(5);
        System.out.println("Is 5 removed? " + value1);

        // Using the removeAll() method
        boolean value2 = numbers.removeAll(numbers);
        System.out.println("Are all elements removed? " + value2);
    }
}

Output:

TreeSet: [2, 5, 6]
Is 5 removed? true
Are all elements removed? true

Methods for Navigation

1. first() and last() Methods

  • first() - returns the first element of the set
  • last() - returns the last element of the set


2. ceiling(), floor(), higher() and lower() Methods

higher(element) - Returns the lowest element among those elements that are greater than the specified element.

lower(element) - Returns the greatest element among those elements that are less than the specified element.

ceiling(element) - Returns the lowest element among those elements that are greater than the specified element. If the element passed exists in a tree set, it returns the element passed as an argument.

floor(element) - Returns the greatest element among those elements that are less than the specified element. If the element passed exists in a tree set, it returns the element passed as an argument.


3. pollfirst() and pollLast() Methods

  • pollFirst() - returns and removes the first element from the set
  • pollLast() - returns and removes the last element from the set


4. headSet(), tailSet() and subSet() Methods

headSet(element, booleanValue)

The headSet() method returns all the elements of a tree set before the specified element (which is passed as an argument).

The booleanValue parameter is optional. Its default value is false.

If true is passed as a booleanValue, the method returns all the elements before the specified element including the specified element.

tailSet(element, booleanValue)

The tailSet() method returns all the elements of a tree set after the specified element (which is passed as a parameter) including the specified element.

The booleanValue parameter is optional. Its default value is true.

If false is passed as a booleanValue, the method returns all the elements after the specified element without including the specified element.

subSet(e1, bv1, e2, bv2)

The subSet() method returns all the elements between e1 and e2 including e1.

The bv1 and bv2 are optional parameters. The default value of bv1 is true, and the default value of bv2 is false.

If false is passed as bv1, the method returns all the elements between e1 and e2 without including e1.

If true is passed as bv2, the method returns all the elements between e1 and e2, including e1.


Other Methods of TreeSet:

MethodDescription
clone()Creates a copy of the TreeSet
contains()Searches the TreeSet for the specified element and returns a boolean result
isEmpty()Checks if the TreeSet is empty
size()Returns the size of the TreeSet
clear()Removes all the elements from the TreeSet