Skip to content

Get TreeSet Element Greater or Less than Specified Element (floor, ceiling) Example

This example shows how to get an element that is greater or less than the specified TreeSet element using the ceiling, floor, higher, and lower methods.

How to get the TreeSet element that is higher or greater than the specified element?

There are a couple of ways using which we can get the TreeSet element that is higher or greater than the specified element.

1. Using the ceiling method

The ceiling method returns the smallest TreeSet element that is greater than or equal to the specified element.

It returns null if there is no such element in the TreeSet.

Output

2. Using the higher method

The higher method returns the smallest TreeSet element that is greater than the specified element.

It returns null if there is no such element in the TreeSet.

Output

How to get the TreeSet element that is lower or less than the specified element?

There are a couple of ways using which we can get the TreeSet element that is lower or less than the specified element.

1. Using the floor method

The floor method returns the largest TreeSet element that is less than or equal to the specified element.

It returns null if there is no such element in the TreeSet.

Output

2. Using the lower method

The lower method returns the largest TreeSet element that is less than the specified element.

It returns null if there is no such element in the TreeSet.

Output

This example is a part of the Java TreeSet Tutorial with Examples.

Please let me know your views in the comments section below.

References:
Java 8 TreeSet

About the author

Leave a Reply

Your email address will not be published.