Java NavigableSet headSet() subSet() tailSet() Methods Tutorial

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

What is Java NavigableSet headSet() Method?

The Java NavigableSet headSet() method is used to get a view of a portion of a NavigableSet 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 either 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 NavigableSet 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 NavigableSet 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 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 NavigableSet 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 NavigableSet subSet() Method?

The Java NavigableSet subSet() method is used to get a view of a sub-set (portion of elements) of a NavigableSet object.

Note: we say a view and not a copy! This means because both the view object (the return value of this method) and the target NavigableSet object are backed by the same array of elements, if one of them changes something in that array, the other will notice.

Java NavigableSet subSet() Method Syntax:

public NavigableSet<E> subSet(E fromElement,

boolean fromInclusive,

E toElement,

boolean toInclusive)

public SortedSet<E> subSet(E fromElement, E toElement)

subSet() Method Parameters:

The method has two variants:

For the first variant, it takes four arguments:

  • The first argument is the element in the set from which we want the view to start.
  • The second argument if a boolean value that shows if the first argument should be inclusive or not. (The value true means first argument is inclusive)
  • The third argument is the element where we want the view to end with.
  • The last argument is a boolean value that shows if the third argument should be inclusive or not. The value true means this argument is in fact inclusive. The value false means otherwise.

For the second variant, it takes only two arguments:

  • The first argument is an element from which we want the view to start. This value is inclusive.
  • The second argument is the element where we want the view to end. This value is exclusive.

Note: The arguments of this method are the actual values in the target NavigableSet object and not their indexes.

subSet() Method Return Value:

For the first variant of this method: The return value of this method is an object of type NavigableSet that is a view of the target NavigableSet object.

But for the second variant of the method, the datatype is SortedSet.

Note: if the first and third arguments of this method are the same (equal number) the return value of this method will be empty unless both the second and fourth arguments are set to true as well.

subSet() Method Exceptions:

The method might throw three types of exceptions:

  • ClassCastException: we get this exception if the first and third arguments of this method cannot be compared to each other because of data type differences.
  • NullPointerException: we get this exception if we put null values as the first or third argument of this method.
  • IllegalArgumentException: we get this exception if the first argument of this method is bigger than the third argument.

Example: using NavigableSet subSet() method

import java.util.TreeSet; 
import java.util.NavigableSet;
import java.util.SortedSet;
class Main{
    public static void main(String[] args) {
        NavigableSet<Integer> ts = new TreeSet<>();
        ts.add(1);
        ts.add(2);
        ts.add(3);
        ts.add(0);
        ts.add(10);
        ts.add(30);
        ts.add(100);
        ts.add(200);
        ts.add(300);
        ts.add(400);
        ts.add(500);

        SortedSet<Integer> view = ts.subSet(10, 500);

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

Output:

10

30

100

200

300

400

What is Java NavigableSet tailSet() Method?

The Java NavigableSet tailSet() method is used to get a view of a portion of a NavigableSet 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 either 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 NavigableSet 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 NavigableSet 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 NavigableSet 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
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies