Java NavigableSet ceiling() floor() Methods Tutorial

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

What is Java NavigableSet ceiling() Method?

The Java NavigableSet ceiling() method is used to get back an element from a NavigableSet object that is equal or 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 NavigableSet ceiling() Method Syntax:

E ceiling(E e)

ceiling() 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.

ceiling() Method Return Value:

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

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

ceiling() 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 target Set does not allow a null value.

Example: using NavigableSet ceiling() method

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

        System.out.println(ts.ceiling(120));
    }
}

Output:

200

What is Java NavigableSet floor() Method?

The Java NavigableSet floor() method is used to get an element in a NavigableSet object that is equal or 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 NavigableSet floor() Method Syntax:

E floor(E e)

floor() Method Parameters:

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

floor() 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.

floor() 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 target Set does not allow a null value.

Example: using NavigableSet floor() method

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

        System.out.println(ts.floor(120));
    }
}

Output:

100

Leave a Reply