java hashmap containskey code example
Example 1: check if map contains key java
if (map.containsKey(key)) {
} else {
}
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"));
}
}