1import java.util.TreeMap;
2public class TreeMapHigherKeyMethodExample
3{
4 public static void main(String[] args)
5 {
6 try
7 {
8 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
9 tm.put(6, "green");
10 tm.put(3, "violet");
11 tm.put(2, "red");
12 tm.put(8, "yellow");
13 tm.put(5, "blue");
14 System.out.println("Given TreeMap: " + tm);
15 // getting higher key value for 5 using higherKey() method
16 int value = tm.higherKey(5);
17 System.out.println("The higherKey value for 5: " + value);
18 }
19 catch(NullPointerException ex)
20 {
21 System.out.println("Exception: " + ex);
22 }
23 }
24}
1Let’s see example on TreeMap higherKey(K key) method for NullPointerException
2import java.util.TreeMap;
3public class TreeMapHigherKeyMethodExample
4{
5 public static void main(String[] args)
6 {
7 try
8 {
9 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
10 tm.put(6, "green");
11 tm.put(3, "violet");
12 tm.put(2, "red");
13 tm.put(8, "yellow");
14 tm.put(5, "blue");
15 System.out.println("Given TreeMap: " + tm);
16 // getting higher key value for null using higherKey() method
17 System.out.println("The higherKey value for null: ");
18 int value = tm.higherKey(null);
19 System.out.println("Value is: " + value);
20 }
21 catch(NullPointerException ex)
22 {
23 System.out.println("Exception: " + ex);
24 }
25 }
26}