Java TreeSet floor() method

The floor() method of TreeSet class is used to find an element which is less than or equal to the given element in the argument list.

Syntax:

Specified by:

floor in interface NavigableSet<E>: In this, the NavigableSet returns the element which is less than or equal to the given element.

Parameter:

e - element to match

Return:

It returns true if the value is less than or equal to the value in the set else it returns false.

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 ]
floor value of 3 in set: 2

Example 2

Output:

TreeSet: [ ALL, BOB, acer, bill ]
Ceiling value of  bi in set: acer

Example 3

Output:

TreeSet: [1, 3, 6, 7]
Ceiling value of 4 in set: 3
Ceiling value of 10 in set: 7