public
Map.Entry<K,V> ceilingEntry(K key)
Returns
a key-value mapping associated with the least key greater than or
equal to the given key, or null if there is no such key.
import java.util.*; class TreeMapCeilingEntry{ public static void main(String args[]){ TreeMap<Integer, String> myMap; Map.Entry<Integer, String> ceilingEntry; myMap = new TreeMap<> (); /* Add Elements to myMap2 */ myMap.put(10, "c"); myMap.put(1, "a"); myMap.put(15, "d"); myMap.put(5, "b"); myMap.put(99, "e"); System.out.println("Elements in myMap are"); System.out.println(myMap); ceilingEntry = myMap.ceilingEntry(6); System.out.print("Ceiling Entry for 6 is "); System.out.println(ceilingEntry); ceilingEntry = myMap.ceilingEntry(100); System.out.print("Ceiling Entry for 100 is "); System.out.println(ceilingEntry); } }
Output
Elements in myMap are {1=a, 5=b, 10=c, 15=d, 99=e} Ceiling Entry for 6 is 10=c Ceiling Entry for 100 is null
1. Throws
NullPointerException if the specified key is null and this map uses
natural ordering, or its comparator does not permit null keys
import java.util.*; class TreeMapCeilingEntryNullPointer{ public static void main(String args[]){ TreeMap<Integer, String> myMap; Map.Entry<Integer, String> ceilingEntry; myMap = new TreeMap<> (); /* Add Elements to myMap2 */ myMap.put(10, "c"); myMap.put(1, "a"); myMap.put(15, "d"); myMap.put(5, "b"); myMap.put(99, "e"); System.out.println("Elements in myMap are"); System.out.println(myMap); System.out.println("Ceiling Entry for null is "); ceilingEntry = myMap.ceilingEntry(null); System.out.println(ceilingEntry); } }
Output
Elements in myMap are {1=a, 5=b, 10=c, 15=d, 99=e} Ceiling Entry for null is Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.compare(TreeMap.java:1290) at java.util.TreeMap.getCeilingEntry(TreeMap.java:397) at java.util.TreeMap.ceilingEntry(TreeMap.java:744) at TreeMapCeilingEntryNullPointer.main(TreeMapCeilingEntryNullPointer.ja va:21)
No comments:
Post a Comment