Java TreeSet lower() method

The lower() is a method of TreeSet class. This method returns the largest element in the set which is smaller than the element in argument, else it will return null if the element is not present.

Syntax:

Specified by:

lower in interface NavigableSet<E> : It returns the largest element in the set which is smaller than the element in the argument.

Parameter:

e - value to match

Return:

It returns the largest element which is smaller than e, else it returns null if there is no such element found.

Throws:

ClassCastException: Throws exception when typecasting is performed in the elements of Set and they are not compatible with each other.

NullPointerException: Throws exception when Set elements are null.

Example 1

Output:

TreeSet: [ 1, 2, 5, 8, 10 ]
Lowest value then 7 : 5

Example 2

Output:

TreeSet: [ D,  Z, a, b ]
Lowest value then a: Z

Example 3

Output:

TreeSet: [ 1, 3, 6, 9, A, B, a, b ]
Lowest value then b : a
Lowest value then 9 : 6