Java NavigableSet lower() higher() Methods Tutorial

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

What is Java NavigableSet lower() Method?

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

For example, let’s say you have a NavigableSet 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 NavigableSet 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 NavigableSet object.

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

Example: using NavigableSet lower() method

import java.util.TreeSet;
import java.util.NavigableSet;
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);

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

Output:

3

What is Java NavigableSet higher() Method?

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

For example, let’s say you have a NavigableSet 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 NavigableSet 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 NavigableSet object.

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

Example: using NavigableSet higher() method

import java.util.TreeSet;
import java.util.NavigableSet;
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);

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

Output:

100

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies