1import java.util.Map;
2import java.util.TreeMap;
3public class TreeMapLowerEntryMethodExample
4{
5 public static void main(String[] args)
6 {
7 try
8 {
9 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
10 tm.put(7, "red");
11 tm.put(3, "green");
12 tm.put(6, "violet");
13 tm.put(5, "blue");
14 tm.put(4, "yellow");
15 System.out.println("Given TreeMap: " + tm);
16 // get lowerEntry value for null using lowerEntry() method
17 System.out.println("Get lowerEntry value for value null: ");
18 Map.Entry<Integer, String> value = tm.lowerEntry(null);
19 System.out.println("Value is: " + value);
20 }
21 catch(NullPointerException ex)
22 {
23 System.out.println("Exception : " + ex);
24 }
25 }
26}
1example on TreeMap lowerKey(K key) method for NullPointerException
2import java.util.TreeMap;
3public class TreeMapLowerKeyMethodExample
4{
5 public static void main(String[] args)
6 {
7 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
8 tm.put(8, "mango");
9 tm.put(5, "apple");
10 tm.put(3, "watermelon");
11 tm.put(7, "pineapple");
12 tm.put(6, "orange");
13 tm.put(9, "grapes");
14 System.out.println("TreeMap: " + tm.toString());
15 try
16 {
17 // pass null as parameter to lowerKey() method
18 System.out.println(tm.lowerKey(null));
19 }
20 catch(Exception ex)
21 {
22 System.out.println("Exception: " + ex);
23 }
24 }
25}
1import java.util.TreeMap;
2public class TreeMapLowerEntryMethodExample
3{
4 public static void main(String[] args)
5 {
6 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
7 tm.put(7, "red");
8 tm.put(3, "green");
9 tm.put(6, "violet");
10 tm.put(5, "blue");
11 tm.put(4, "yellow");
12 // get lower entry
13 System.out.println("Check lower entry in given TreeMap");
14 System.out.println("Value is: "+ tm.lowerEntry(5));
15 }
16}
1import java.util.TreeMap;
2public class TreeMapLowerKeyMethodExample
3{
4 public static void main(String[] args)
5 {
6 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
7 tm.put(8, "mango");
8 tm.put(5, "apple");
9 tm.put(3, "watermelon");
10 tm.put(7, "pineapple");
11 tm.put(6, "orange");
12 tm.put(9, "grapes");
13 System.out.println("TreeMap: " + tm.toString());
14 // here 10 is not available it returns 9
15 System.out.print("Lower Key Entry of Element 10 is: ");
16 System.out.println(tm.lowerKey(10));
17 System.out.print("Lower Key Entry of Element 5 is: ");
18 System.out.println(tm.lowerKey(5));
19 }
20}