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.
1 |
public E ceiling(E e) |
It returns null if there is no such element in the TreeSet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(1); tSetOddNumbers.add(3); tSetOddNumbers.add(5); tSetOddNumbers.add(7); tSetOddNumbers.add(9); /* * The ceiling method returns lowest element that * is greater than or equal to the specified element */ //returns 1 i.e. element >= 1 System.out.println( tSetOddNumbers.ceiling(1) ); //returns 3 i.e. element >= 2 System.out.println( tSetOddNumbers.ceiling(2) ); //returns null as there is no element that is >= 10 System.out.println( tSetOddNumbers.ceiling(10) ); |
Output
1 2 3 |
1 3 null |
2. Using the higher method
The higher
method returns the smallest TreeSet element that is greater than the specified element.
1 |
public E higher(E e) |
It returns null if there is no such element in the TreeSet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(1); tSetOddNumbers.add(3); tSetOddNumbers.add(5); tSetOddNumbers.add(7); tSetOddNumbers.add(9); /* * The higher method returns the lowest element * that is greater than specified element */ //returns 1 i.e. element > 0 System.out.println( tSetOddNumbers.higher(0) ); //returns 3 i.e. element > 1 System.out.println( tSetOddNumbers.higher(1) ); //returns 3 i.e. element > 2 System.out.println( tSetOddNumbers.higher(2) ); //returns null as there is no element that is > 9 System.out.println( tSetOddNumbers.higher(9) ); |
Output
1 2 3 4 |
1 3 3 null |
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.
1 |
public E floor(E e) |
It returns null if there is no such element in the TreeSet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(1); tSetOddNumbers.add(3); tSetOddNumbers.add(5); tSetOddNumbers.add(7); tSetOddNumbers.add(9); /* * The floor method returns the largest element * that is less than or equal to * the specified element */ //returns null there is no element that is <= 0 System.out.println( tSetOddNumbers.floor(0) ); //returns 1 i.e. element <= 1 System.out.println( tSetOddNumbers.floor(1) ); //returns 1 i.e. element <= 2 System.out.println( tSetOddNumbers.floor(2) ); //returns 9 i.e. element <= 9 System.out.println( tSetOddNumbers.floor(9) ); |
Output
1 2 3 4 |
null 1 1 9 |
2. Using the lower method
The lower
method returns the largest TreeSet element that is less than the specified element.
1 |
public E lower(E e) |
It returns null if there is no such element in the TreeSet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
TreeSet<Integer> tSetOddNumbers = new TreeSet<Integer>(); tSetOddNumbers.add(1); tSetOddNumbers.add(3); tSetOddNumbers.add(5); tSetOddNumbers.add(7); tSetOddNumbers.add(9); /* * The lower method returns the largest element * that is less than the specified element */ //returns null there is no element that is < 1 System.out.println( tSetOddNumbers.lower(1) ); //returns 1 i.e. element < 2 System.out.println( tSetOddNumbers.lower(2) ); //returns 7 i.e. element < 9 System.out.println( tSetOddNumbers.lower(9) ); //returns 9 i.e. element < 10 System.out.println( tSetOddNumbers.lower(10) ); |
Output
1 2 3 4 |
null 1 7 9 |
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