Java TreeSet ceiling() floor() Methods Tutorial

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

What is Java TreeSet ceiling() Method?

The Java TreeSet ceiling() method is used to get back an element from a TreeSet object that is equal or 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 TreeSet 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 TreeSet object with.

ceiling() Method Return Value:

The return value of this method is an element in the Set that is the equal or 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 TreeSet 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 TreeSet 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 TreeSet floor() Method?

The Java TreeSet floor() method is used to get an element in a TreeSet object that is equal or 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 TreeSet 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 TreeSet object with.

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

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 TreeSet 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 TreeSet 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

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies