Java TreeSet lower() higher() Methods Tutorial

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

What is Java TreeSet lower() Method?

The Java TreeSet lower() method is used to get the greatest element in a TreeSet object that is lower than the specified element in the argument.

For example, let’s say you have a TreeSet object with the given values:

2,5,6,8,10.

Now if you call this method and put the value 9 for example, the return value of this method will be 8. Because this is the greatest element in the list, that is also less than the value 9.

Java lower() Method Syntax:

public E lower(E e)

lower() Method Parameters:

The method takes on argument only and that is the element we want to match the elements in the target TreeSet object with.

lower() Method Return Value:

The return value of this method is the greatest element in a TreeSet that is also less than then specified argument.

Note: We will get the value null if there’s no such element.

lower() Method Exceptions:

The method might throw two exceptions:

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

NullPointerException: We get this exception if the argument of the method is null and the list is in natural order.

Example: using TreeSet lower() method

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

        System.out.println(navigate.lower(23));
    }
        
}

Output:

3

What is Java TreeSet higher() Method?

The Java TreeSet higher() method is used to get back an element from a TreeSet object that is least greater than the specified argument of this method.

For example, let’s say you have a TreeSet object with these elements:

3,14,555,68,71,88,99

Now if you put the value 76 as the argument of this method, you’ll get the value 88 because this value is the least greater element in this Set compared to the rest of elements.

Java higher() Method Syntax:

public E higher(E e)

higher() Method Parameters:

The method takes one argument and that is the value we want to match the rest of elements in a TreeSet object with.

higher() Method Return Value:

The return value of this method is an element in the Set that is least greater than the specified argument.

Note: We will get the value null if there’s no such element.

higher() Method Exceptions:

The method might throw two exceptions:

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

NullPointerException: We get this exception if the argument of the method is null and the list is in natural order.

Example: using TreeSet higher() method

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

        System.out.println(navigate.higher(23));
    }
        
}

Output:

100

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies