Java TreeSet ceiling() method

The ceiling() is a method of TreeSet class which is used to find element which is greater than or equal to the given element in the argument list.

Syntax:

Specified by:

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

Parameter:

e - element to match

Return:

It returns true if the value is greater 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 ]
Ceiling value of 4 in set: 5

Example 2

Output:

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

Example 3

Output:

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