treeMap contains key string code example
Example 1: TreeMap containsKey() method in java
import java.util.TreeMap;
public class TreeMapContainsKeyMethodExample
{
public static void main(String[] args)
{
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(56, "orange");
tm.put(62, "indigo");
tm.put(43, "red");
tm.put(91, "green");
tm.put(38, "yellow");
System.out.println("TreeMap before using containsKey() method: " + tm);
System.out.println("Does key '62' present? " + tm.containsKey(62));
System.out.println("Does key '90' present? " + tm.containsKey(90));
}
}
Example 2: TreeMap containsKey() method in java
map Integer values to String Keys
import java.util.TreeMap;
public class TreeMapContainsKeyMethodExample
{
public static void main(String[] args)
{
TreeMap<String, Integer> tm = new TreeMap<String, Integer>();
tm.put("violet", 18);
tm.put("red", 12);
tm.put("violet", 14);
tm.put("green", 16);
tm.put("blue", 20);
System.out.println("Given TreeMap Mappings are: " + tm);
System.out.println("Is key 'green' present? " + tm.containsKey("green"));
System.out.println("Is key 'yellow' present? " + tm.containsKey("yellow"));
}
}