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

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
        }
    }
}

chevron_right


Output:

Ceiling key entry for 5: 6

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


Output:

Ceiling key entry for 5: 6

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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));
    }
}

chevron_right


Output:

Ceiling key entry for 10: null

Program 4: To show NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
        }
    }
}

chevron_right


Output:

Exception: java.lang.NullPointerException

Program 5: To demonstrate ClassCastException

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.