We will learn about treemap ceilingKey () method in java in this tutorial . Java TreeMap ceilingKey() method 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 : This is the given key to be matched.
Return Value : It’s return the least key greater than or equal to key, or null if there is no such key .
Exception : Java ThreeMap ceilingkey () method throws following exceptions .
ClassCastException – if the given key cannot be compared with the keys in the map
NullPointerException – if the given key is null and this map uses natural ordering, or its comparator does not permit null keys.
Now we will discuss different examples of ceilingKey() method .
Example 1 : Java TreeMap ceilingKey() method – Return least greater than or equal Key
In this example we create TreeMap and add element in it . Now we use ceilingKey() method with 9,7,2 and 1 key . Because here maximum value of key is 9 , So for ceilingKey(10) it returns 9 . for ceilingKey(7) it returns 9 because 9 is least greater than key in TreeMap for key 7. for ceilingKey(2) , its return 2 and floorKey(1) , it’s return 2 .
import java.util.TreeMap;
public class JavaTreeMapceilingKey {
public static void main(String[] args) {
TreeMap<Integer,String> treeMap = new TreeMap<Integer,String>();
treeMap.put(2, "vogue");
treeMap.put(3,"welcomes");
treeMap.put(4,"you");
treeMap.put(6,"java");
treeMap.put(5,"map");
treeMap.put(9,"php");
//print TreeMap Elements
System.out.println("TreeMap Elements : "+ treeMap);
//print ceilingKey For key 9
System.out.println("treeMap.ceilingKey(9) : "+ treeMap.ceilingKey(9));
//print ceilingKey For key 7
System.out.println("treeMap.ceilingKey(7) : "+ treeMap.ceilingKey(7));
//print ceilingKey For key 2
System.out.println("treeMap.ceilingKey(2) : "+ treeMap.ceilingKey(2));
//print ceilingKey For key 1
System.out.println("treeMap.ceilingKey(1) : "+ treeMap.ceilingKey(1));
}
}
Output:
TreeMap Elements : {2=vogue, 3=welcomes, 4=you, 5=map, 6=java, 9=php}
treeMap.ceilingKey(9) : 9
treeMap.ceilingKey(7) : 9
treeMap.ceilingKey(2) : 2
treeMap.ceilingKey(1) : 2
Example 2: Java TreeMap ceilingKey() method – Return null key
TreeMap ceilingKey() method will return null key if there is no least greater than or equal to key of given key. Greater key value is 9 in this example for TreeMap . So treeMap.ceilingKey(10) and treeMap.ceilingKey(11) will return null in this example.
import java.util.TreeMap;
public class JavaTreeMapceilingKey1 {
public static void main(String[] args) {
TreeMap<Integer,String> treeMap = new TreeMap<Integer,String>();
treeMap.put(1,"java");
treeMap.put(2, "vogue");
treeMap.put(3,"welcomes");
treeMap.put(4,"you");
treeMap.put(5,"map");
treeMap.put(9,"php");
//print TreeMap Elements
System.out.println("TreeMap Elements : "+ treeMap);
//print ceilingKey For key 10
System.out.println("treeMap.ceilingKey(10) : "+ treeMap.ceilingKey(10));
//print ceilingKey For key 11
System.out.println("treeMap.ceilingKey(11) : "+ treeMap.ceilingKey(11));
//print ceilingKey For key 30
System.out.println("treeMap.ceilingKey(30) : "+ treeMap.ceilingKey(30));
}
}
Output :
TreeMap Elements : {1=java, 2=vogue, 3=welcomes, 4=you, 5=map, 9=php}
treeMap.ceilingKey(10) : null
treeMap.ceilingKey(11) : null
treeMap.ceilingKey(30) : null
Exaample 3 : Java TreeMap ceilingKey() method – NullPointerException
We will use null key then TreeMap ceilingKey() method will throw null pointer exception
import java.util.TreeMap;
public class JavaTreeMapceilingKey2 {
public static void main(String[] args) {
TreeMap<Integer,String> treeMap = new TreeMap<Integer,String>();
treeMap.put(1,"java");
treeMap.put(2, "vogue");
treeMap.put(3,"welcomes");
treeMap.put(4,"you");
treeMap.put(5,"map");
treeMap.put(9,"php");
//print TreeMap Elements
System.out.println("TreeMap Elements : "+ treeMap);
try{
System.out.println(" "+ treeMap.ceilingKey(null));
}catch(Exception e){
System.out.println("Exception : "+ e);
}
}
}
Output :
TreeMap Elements : {1=java, 2=vogue, 3=welcomes, 4=you, 5=map, 9=php}
Exception : java.lang.NullPointerException
Example 4 : Java TreeMap ceilingKey() method – ClassCastException
Java TreeMap ceilingKey() method throws ClassCastException if the given key cannot be compared with the keys in the map . Let’s see java treemap ceilingkey example for ClassCastException .
import java.util.TreeMap;
public class JavaTreeMapceilingKey3 {
public static void main(String[] args) {
TreeMap<Object,String> treeMap = new TreeMap<Object,String>();
treeMap.put(1,"java");
treeMap.put(2, "vogue");
treeMap.put(3,"welcomes");
treeMap.put(4,"you");
treeMap.put(5,"map");
treeMap.put(9,"php");
//print TreeMap Elements
System.out.println("TreeMap Elements : "+ treeMap);
try{
System.out.println(" "+ treeMap.ceilingKey(new String("JavaMap") ));
}catch(Exception e){
System.out.println("Exception : "+ e);
}
}
}
Output:
TreeMap Elements : {1=java, 2=vogue, 3=welcomes, 4=you, 5=map, 9=php}
Exception : java.lang.ClassCastException: java.lang.Integer cannot
be cast to java.lang.String
In this tutorial we have learned java treemap ceilingKey() method . you can see more good java TreeMap Examples for practice .