1import java.util.TreeMap;
2public class TreeMapContainsKeyMethodExample
3{
4 public static void main(String[] args)
5 {
6 TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
7 // map String values to Integer keys
8 tm.put(56, "orange");
9 tm.put(62, "indigo");
10 tm.put(43, "red");
11 tm.put(91, "green");
12 tm.put(38, "yellow");
13 System.out.println("TreeMap before using containsKey() method: " + tm);
14 // check for key element '62'
15 System.out.println("Does key '62' present? " + tm.containsKey(62));
16 // check for key element '90'
17 System.out.println("Does key '90' present? " + tm.containsKey(90));
18 }
19}
1map Integer values to String Keys
2import java.util.TreeMap;
3public class TreeMapContainsKeyMethodExample
4{
5 public static void main(String[] args)
6 {
7 TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
8 // Mapping Integer values to string keys
9 tm.put("violet", 18);
10 tm.put("red", 12);
11 tm.put("violet", 14);
12 tm.put("green", 16);
13 tm.put("blue", 20);
14 System.out.println("Given TreeMap Mappings are: " + tm);
15 // check for key element 'green'
16 System.out.println("Is key 'green' present? " + tm.containsKey("green"));
17 // checking for key element 'yellow'
18 System.out.println("Is key 'yellow' present? " + tm.containsKey("yellow"));
19 }
20}