Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaTreeSet higher() method in Java with Examples

TreeSet higher() method in Java with Examples

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.

Syntax:

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.

Exceptions:

  • 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 1:




// Java program to illustrate the
// TreeSet higher() method
  
import java.util.TreeSet;
public class GFG {
    public static void main(String args[])
    {
        TreeSet<Integer> tree = new TreeSet<Integer>();
        tree.add(10);
        tree.add(5);
        tree.add(8);
        tree.add(1);
        tree.add(11);
        tree.add(3);
  
        System.out.println(tree.higher(10));
    }
}


Output:

11

Program 2:




// Java program to illustrate the
// TreeSet higher() method
  
import java.util.TreeSet;
public class GFG {
    public static void main(String args[])
    {
        TreeSet<Integer> tree = new TreeSet<Integer>();
  
        tree.add(10);
        tree.add(5);
        tree.add(8);
        tree.add(1);
        tree.add(11);
        tree.add(3);
  
        System.out.println(tree.higher(15));
    }
}


Output:

null

Program 3: Program to demonstrate the NullPointerException.




// Java program to illustrate the
// TreeSet higher() method
  
import java.util.TreeSet;
public class GFG {
    public static void main(String args[])
    {
        TreeSet<String> tree = new TreeSet<String>();
  
        tree.add("10");
        tree.add("5");
        tree.add("8");
        tree.add("1");
        tree.add("11");
        tree.add("3");
  
        // Pass a NULL to the method
        try {
            System.out.println(tree.higher(null));
        } // Catch the Exception
        catch (Exception e) {
  
            // Print the Exception
            System.out.println(e);
        }
    }
}


Output:

java.lang.NullPointerException

Program 4: Demonstrate the ClassCastException.




// Java program to illustrate the
// TreeSet higher() method
  
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeSet;
  
public class GFG {
    public static void main(String args[])
    {
        TreeSet<List> tree = new TreeSet<List>();
        List<Integer> l1 = new LinkedList<Integer>();
  
        try {
  
            l1.add(1);
            l1.add(2);
            tree.add(l1);
  
            List<Integer> l2 = new LinkedList<Integer>();
            l2.add(3);
            l2.add(4);
  
            List<Integer> l3 = new ArrayList<Integer>();
            l2.add(5);
            l2.add(6);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.lang.Comparable

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#higher(E)

RELATED ARTICLES

Most Popular

Recent Comments