Java TreeSet higher() method

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

Syntax:

Specified by:

higher in interface NavigableSet<E> : It returns the smallest element in the set which is greater than the element in the argument

Parameter:

e - value to match

Return:

It returns the smallest element which is greater 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 ]
Highest value then 7 : 8

Example 2

Output:

TreeSet: [ D,  Z, a, b ]
Highest value then C : D

Example 3

Output:

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