TreeMap ceilingKey() method in java

Let’s learn TreeMap ceilingKey() method in java.

TreeMap ceilingKey() method in java

ceilingKey(K key) method of TreeMap class returns the least key greater than or equal to the given key, or null if there is no such key.

Syntax:

public K ceilingKey(K key)

Parameters:

key the key

Throws:

ClassCastException – if the specified key cannot be compared with the keys currently in the map.

NullPointerException – if the specified key is null and this map does not permit null keys.

Also read – TreeMap in java

Now let’s see example on TreeMap ceilingKey(K key) method.

import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapCeilingkeyDemo
{
   public static void main(String[] args)
   {
      NavigableMap<Integer, String> nm = new TreeMap<Integer, String>();
      nm.put(10, "apple");
      nm.put(20, "banana");
      nm.put(30, "cherry");
      nm.put(40, "fig");
      nm.put(60, "grape");
      nm.put(70, "kiwifruit");
      // 60 is least value > 50
      // it is returned as key.
      System.out.println("Ceiling key for 50: " + nm.ceilingKey(50));
   }
}


Output:

Ceiling key for 50: 60


Let’s see example of TreeMap ceilingKey(K key) method when it returns null.

import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapCeilingkeyDemo
{
   public static void main(String[] args)
   {
      NavigableMap<Integer, String> nm = new TreeMap<Integer, String>();
      nm.put(10, "apple");
      nm.put(20, "banana");
      nm.put(30, "cherry");
      nm.put(40, "fig");
      nm.put(60, "grape");
      nm.put(70, "kiwifruit");
      // 200 is not present in map
      // or any key greater than 200
      // hence returns null
      System.out.println("Ceiling key for 200: " + nm.ceilingKey(200));
   }
}


Output:

Ceiling key for 200: null


Here’s example on TreeMap ceilingKey(K key) method when it returns NullPointerException.

import java.util.TreeMap;
public class TreeMapCeilingkeyDemo
{
   public static void main(String[] args)
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
      tm.put(10, "apple");
      tm.put(20, "banana");
      tm.put(30, "cherry");
      tm.put(40, "fig");
      tm.put(60, "grape");
      tm.put(70, "kiwifruit");
      try
      {
         // returns a NullPointerException as key value cannot be null
         System.out.println("Ceiling key entry for null value is : " + tm.ceilingKey(null));
      }
      catch(Exception ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}


Output:

Exception: java.lang.NullPointerException


Now let’s see example on TreeMap ceilingKey(K key) method when it returns ClassCastException.

import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapCeilingkeyDemo
{
   public static void main(String[] args)
   {
      NavigableMap<Object, String> tm = new TreeMap<Object, String>();
      tm.put(10, "apple");
      tm.put(20, "banana");
      tm.put(30, "cherry");
      tm.put(40, "fig");
      tm.put(60, "grape");
      tm.put(70, "kiwifruit");
      try
      {
         // returns ClassCastException
         // we cannot compare String object with an Integer object
         System.out.println("Ceiling key entry for \"asd\": " + tm.ceilingKey(new String("mango")));
      }
      catch(Exception ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}


Output:

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