TreeMap ceilingKey() in Java with Examples

The ceilingKey() function of TreeMap Class returns the least key greater than or equal to the given key or null if the such a key is absent.

Syntax:

public K ceilingKey(K key)

Parameters: This method accepts a mandatory parameter key which is the key to be searched for.

Return Value: This method returns the least key which is greater than or equal to the given key value.
If such a key is absent, null is returned.

Exceptions: This method throws following exceptions:

  • ClassCastException – Thrown if the specified key can’t be compared with the given key values.
  • NullPointerException – Thrown if the given key is null and the map uses natural ordering or the comparator does not permit null values.

Below are the examples to illustrate ceilingKey() method:

Program 1: To demonstrate use of ceilingKey() method for a TreeMap with comparator




import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
  
        // creating tree map
        NavigableMap<Integer, String>
            treemap = new TreeMap<Integer,
                                  String>((a, b)
                                              -> ((a > b)
                                                      ? 1
                                                      : ((a == b)
                                                             ? 0
                                                             : -1)));
  
        // populating tree map
        treemap.put(1, " A ");
        treemap.put(2, " B ");
        treemap.put(3, " C ");
        treemap.put(4, " D ");
        treemap.put(6, " E ");
        try {
            System.out.println("Ceiling key entry for 5: "
                               + treemap.ceilingKey(5));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Ceiling key entry for 5: 6

Program 2: To demonstrate use of ceilingKey() method for a TreeMap without any comparator




import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
  
        // creating tree map
        NavigableMap<Integer, String>
            treemap = new TreeMap<Integer, String>();
  
        // populating tree map
        treemap.put(1, " A ");
        treemap.put(2, " B ");
        treemap.put(3, " C ");
        treemap.put(4, " D ");
        treemap.put(6, " E ");
        treemap.put(7, " F ");
  
        // Since 6 is the least value greater than 5,
        // it is returned as the key.
        System.out.println("Ceiling key entry for 5: "
                           + treemap.ceilingKey(5));
    }
}


Output:

Ceiling key entry for 5: 6

Program 3: To demonstrate use of ceilingKey() method when it will return null




import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
  
        // creating tree map
        NavigableMap<Integer, String>
            treemap = new TreeMap<Integer, String>();
  
        // populating tree map
        treemap.put(1, " A ");
        treemap.put(2, " B ");
        treemap.put(3, " C ");
        treemap.put(4, " E ");
        treemap.put(5, " D ");
  
        // Since 10 is not present in the Map
        // and neither any Key is present greater than 10
        // Hence this will return null
        System.out.println("Ceiling key entry for 10: "
                           + treemap.ceilingKey(10));
    }
}


Output:

Ceiling key entry for 10: null

Program 4: To show NullPointerException




import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
  
        // creating tree map
        TreeMap<Integer, String>
            treemap = new TreeMap<Integer, String>();
  
        // populating tree map
        treemap.put(2, " two ");
        treemap.put(1, " one ");
        treemap.put(3, " three ");
        treemap.put(6, " six ");
        treemap.put(5, " five ");
  
        try {
            // returns a NullPointerException
            // as key value can't be null
            // because of natural ordering
            System.out.println("Ceiling key entry for null value : "
                               + treemap.ceilingKey(null));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.NullPointerException

Program 5: To demonstrate ClassCastException




import java.util.*;
  
public class Main {
    public static void main(String[] args)
    {
  
        // creating tree map
        NavigableMap<Object, String>
            treemap = new TreeMap<Object, String>();
  
        // populating tree map
        treemap.put(1, " A ");
        treemap.put(2, " B ");
        treemap.put(3, " C ");
        treemap.put(4, " E ");
        treemap.put(5, " D ");
  
        try {
            // returns ClassCastException
            // as we cannot compare a String object with an Integer object
            System.out.println("Ceiling key entry for \"asd\": "
                               + treemap.ceilingKey(new String("asd")));
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


Output:

Exception: java.lang.ClassCastException: 
           java.lang.Integer cannot be cast to java.lang.String