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

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *

Online Shopping