The higher(E ele) method of TreeSet class in Java is used to return the least element in this set which is strictly greater than the given element ele. If no such element is there then this method returns NULL.
Here, E is the type of element maintained by this TreeSet collection.
public E higher(E ele)
Parameters:It takes only one parameter ele. It is the element based on which the least value in the set which is strictly greater than this value is determined.
Return Value: It returns a value of type which this TreeSet stores which is either null or the required value.
- ClassCastException: This method throws a ClassCastException if the specified element cannot be compared with the elements of the set.
- NullPointerException: This method throws a NullPointerException if the given element is null and the set uses natural ordering or the comparator does not permit null values.
Below programs illustrate the above method:
Program 3: Program to demonstrate the NullPointerException.
Program 4: Demonstrate the ClassCastException.
java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.lang.Comparable
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.