Java TreeSet headSet() tailSet() Methods Tutorial

In this section, we will learn what the TreeSet headSet(), subSet(), and tailSet() methods are and how to use them in Java.

What is Java TreeSet headSet() Method?

The Java TreeSet headSet() method is used to get a view of a portion of a TreeSet object.

The method takes one argument, and that is the element that defines the greatest value in the returned view. Basically, this value means the rest of returned values from the object should be equal or lower than the specified argument of the method.

Note that we say a “view” and not a copy! This is because both the view and the TreeSet object point to the same underlying array of elements. So this means if one changed an element in the array, the other will see this modification as well.

Java TreeSet headSet() Method Syntax:

NavigableSet<E> headSet(E fromElement, boolean inclusive)

SortedSet<E> headSet(E fromElement)

headSet() Method Parameters:

The method has two variants:

For the first variant, the first argument is the value that specifies the greatest element of the view. Basically, this argument specifies that the rest of elements in the view should either be lower or equal to this argument.

The second argument of this method a Boolean value that defines if the first argument should be inclusive or exclusive. The value true means inclusive and false means otherwise.

For the second variant, the method takes only one argument and that is the greatest element of the view (Just like the first argument of the first variant). Also, this value is inclusive.

headSet() Method Return Value:

The return value of this method is a view of a portion of the target Set object whose elements are greater or equal to the argument of this method.

headSet() Method Exceptions:

ClassCastException: We get this exception if the argument data type is of incompatible type compared to the types of elements in the target Set object.

NullPointerException: We get this exception if the target Set object does not allow a null value as its element but we specify a null value as the argument of the method, anyway.

IllegalArgumentException: We get this exception if the Set object has a restricted range, and fromElement lies outside the bounds of the range.

Example: using TreeSet headSet() method

import java.util.TreeSet;
import java.util.NavigableSet;
import java.util.SortedSet;
class Main{
    public static void main(String[] args) {
        NavigableSet<Integer> navigate = new TreeSet<>();
        navigate.add(100);
        navigate.add(2);
        navigate.add(3);
        navigate.add(0);
        navigate.add(23);

        SortedSet<Integer> sorted = navigate.headSet(24,true);

        for (int i :sorted){
            System.out.println(i);
        }
    }
        
}

Output:

0

2

3

23

What is Java TreeSet tailSet() Method?

The Java TreeSet tailSet() method is used to get a view of a portion of a TreeSet object.

The method takes one argument and that is the element that defines the least value in the returned view. Basically, this value means the rest of returned values from the object should be equal or higher than the specified argument of the method.

Note that we say a “view” and not a copy! This is because both the view and the TreeSet object point to the same underlying array of elements. So this means if one changed an element in the array, the other will see this change as well.

Java TreeSet tailSet() Method Syntax:

NavigableSet<E> tailSet(E fromElement, boolean inclusive)

SortedSet<E> tailSet(E fromElement)

tailSet() Method Parameters:

The method has two variants:

For the first variant, the first argument is the value that specifies the least element of the view. Basically, this argument specifies that the rest of elements in the view should either be greater or equal to this argument.

The second argument of this method a Boolean value that defines if the first argument should be inclusive or exclusive. The value true means inclusive and false means otherwise.

For the second variant, the method takes only one argument and that is the least element of the view (Just like the first argument of the first variant). Also, this value is inclusive.

tailSet() Method Return Value:

The return value of this method is a view of a portion of the target Set object whose elements are greater or equal to the argument of this method.

tailSet() Method Exceptions:

ClassCastException: We get this exception if the argument datatype is of incompatible type compared to the types of elements in the target Set object.

NullPointerException: We get this exception if the target Set object does not allow a null value as its element but we specify a null value as the argument of the method, anyway.

IllegalArgumentException: We get this exception if the Set object has a restricted range, and fromElement lies outside the bounds of the range.

Example: using TreeSet tailSet() method

import java.util.TreeSet;
import java.util.NavigableSet;
import java.util.SortedSet;
class Main{
    public static void main(String[] args) {
        NavigableSet<Integer> navigate = new TreeSet<>();
        navigate.add(100);
        navigate.add(2);
        navigate.add(3);
        navigate.add(0);
        navigate.add(23);

        SortedSet<Integer> sorted = navigate.tailSet(3,true);

        for (int i :sorted){
            System.out.println(i);
        }
    }
        
}

Output:

3

23

100

Leave a Reply