Java HashMap containsKey() method is used to check a key exits or not in HashMap. HashMap.containsKey() Method returns true if this java map contains a mapping for the given key. More formally, returns true if and only if this java map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
Syntax Of HashMap containsKey() Method
This is syntax of map.containskey() method in java
public boolean containsKey(Object key) {
}
Parameters – key key whose presence in this map is to be tested
Return Value – true if this map contains a mapping for the given key
Exception – Its throws following exception
ClassCastException – if the key is of an inappropriate type for this map (optional)
NullPointerException – if the specified key is null and this map does not permit null keys (optional)
Example 1 : Java Hashmap containsKey() method -Value present
In this example we will create java HashMap with String and Integer mapping . We will add values in HashMap using put() method. We check the key “vogue” and “welcomes” are present or not using map containskey() method .Because java map contains these keys , it will returns true .
import java.util.HashMap;
import java.util.Map;
public class HashMapContainsKey {
public static void main(String[] args) {
Map<String,Integer> hm = new HashMap<String,Integer>();
hm.put("java",1);
hm.put("vogue",2);
hm.put("welcomes",3);
hm.put("you",4);
System.out.println("is key 'vogue' present = "+hm.containsKey("vogue"));
System.out.println("is key 'welcomes' present = "+hm.containsKey("welcomes"));
}
}
Output:
is key 'vogue' present = true
is key 'welcomes' present = true
Example 2 : Java Hashmap containsKey() method -Value not present
We will create java HashMap with String and Integer mapping . We will add values in HashMap using put() method. We check the key “tutorial” and “hashmap” are present or not using HashMap containskey() method . Here , java map does not contain these keys , it will returns false .
import java.util.HashMap;
import java.util.Map;
public class HashMapContainsKey1 {
public static void main(String[] args) {
Map<String,Integer> hm = new HashMap<String,Integer>();
hm.put("java",1);
hm.put("vogue",2);
hm.put("welcomes",3);
hm.put("you",4);
System.out.println("is key 'tutorial' present = "+hm.containsKey("tutorial"));
System.out.println("is key 'hashmap' present = "+hm.containsKey("hashmap"));
}
}
Output:
is key 'tutorial' present = false
is key 'hashmap' present = false
Example 3 : Hashmap containsKey() method – Mapping String to Integer keys
In this example we will create HashMap with Integer and String mapping . We add element in map using put method . We will check java map contains keys using map.containskey() method in java. Here , we check key 1 and “2” is present or not in HashMap . We use hashmap.containsKey() Method for key 1 it will returns true and for key “2” it will returns false , because key “2” is a string not a integer .
import java.util.HashMap;
import java.util.Map;
public class HashMapContainsKey2 {
public static void main(String[] args) {
Map<Integer,String> hm = new HashMap<Integer,String>();
hm.put(1,"java");
hm.put(2, "vogue");
hm.put(3,"welcomes");
hm.put(4,"you");
System.out.println("is key '1' present = "+hm.containsKey(1));
System.out.println("is key '2' present = "+hm.containsKey("2"));
}
}
OutPut:
is key '1' present = true
is key '2' present = false
In this tutorial, We have learned java hashmap containskey() method . By using map.containskey() we can check java map contains specific key or not. It is very useful when we have to check if a key exists in hashmap java or not . You can see more java HashMap program for practice .