Given a HashMap and a key in Java, the task is to check if this key exists in the HashMap or not.
Examples:
Input: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, key = 2 Output: true Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 10 Output: false
- Using Iterator (Not Efficient):
- Get the HashMap and the Key
- Create an iterator to iterate over the HashMap using HashMap.iterate() method.
- Iterate over the HashMap using the Iterator.hasNext() method.
- While iterating, check for the key at that iteration to be equal to the key specified. The entry key of the Map can be obtained with the help of entry.getKey() method.
- If the key matches, set the flag as true.
- The flag value after iterating, contains the result.
Below is the implementation of the above approach:
Program 1:
// Java program to check if a key exists
// in a HashMap or not
Â
Âimport
java.util.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Create a HashMap
       Â
HashMap<Integer, String>
           Â
map =
new
HashMap<>();
Â
ÂÂ Â Â Â Â Â Â Â
// Populate the HashMap
       Â
map.put(
1
,
"Geeks"
);
       Â
map.put(
2
,
"ForGeeks"
);
       Â
map.put(
3
,
"GeeksForGeeks"
);
Â
ÂÂ Â Â Â Â Â Â Â
// Get the key to be removed
       Â
int
keyToBeChecked =
2
;
Â
ÂÂ Â Â Â Â Â Â Â
// Print the initial HashMap
       Â
System.out.println(
"HashMap: "
                          Â
+ map);
Â
ÂÂ Â Â Â Â Â Â Â
// Get the iterator over the HashMap
       Â
Iterator<Map.Entry<Integer, String> >
           Â
iterator = map.entrySet().iterator();
Â
ÂÂ Â Â Â Â Â Â Â
// flag to store result
       Â
boolean
isKeyPresent =
false
;
Â
ÂÂ Â Â Â Â Â Â Â
// Iterate over the HashMap
       Â
while
(iterator.hasNext()) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// Get the entry at this iteration
           Â
Map.Entry<Integer, String>
               Â
entry
               Â
= iterator.next();
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// Check if this key is the required key
           Â
if
(keyToBeChecked == entry.getKey()) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
isKeyPresent =
true
;
           Â
}
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
// Print the result
       Â
System.out.println(
"Does key "
                          Â
+ keyToBeChecked
                          Â
+
" exists: "
                          Â
+ isKeyPresent);
   Â
}
}
Output:HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks} Does key 2 exists: true
Program 2: To show why this method is not suggested. If the HashMap contains null values, then this method will throw NullPointerException. This makes this method a non suggestable method to use.
// Java program to check if a key exists
// in a HashMap or not
Â
Âimport
java.util.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
try
{
           Â
// Create a HashMap
           Â
HashMap<Integer, String>
               Â
map =
new
HashMap<>();
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// Populate the HashMap
           Â
map.put(
1
,
"Geeks"
);
           Â
map.put(
2
,
"ForGeeks"
);
           Â
map.put(
null
,
"GeeksForGeeks"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// Get the key to be removed
           Â
int
keyToBeChecked =
2
;
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// Print the initial HashMap
           Â
System.out.println(
"HashMap: "
                              Â
+ map);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// Get the iterator over the HashMap
           Â
Iterator<Map.Entry<Integer, String> >
               Â
iterator = map.entrySet().iterator();
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// flag to store result
           Â
boolean
isKeyPresent =
false
;
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// Iterate over the HashMap
           Â
while
(iterator.hasNext()) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
// Get the entry at this iteration
               Â
Map.Entry<Integer, String>
                   Â
entry
                   Â
= iterator.next();
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
// Check if this key is the required key
               Â
if
(keyToBeChecked == entry.getKey()) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
isKeyPresent =
true
;
               Â
}
           Â
}
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// Print the result
           Â
System.out.println(
"Does key "
                              Â
+ keyToBeChecked
                              Â
+
" exists: "
                              Â
+ isKeyPresent);
       Â
}
       Â
catch
(Exception e) {
           Â
System.out.println(e);
       Â
}
   Â
}
}
Output:HashMap: {null=GeeksForGeeks, 1=Geeks, 2=ForGeeks} java.lang.NullPointerException
- Using HashMap.containsKey method(Efficient):
- Get the HashMap and the Key
- Check if the key exists in the HashMap or not using HashMap.containsKey() method. If the key exists, set the flag as true.
- The flag value, contains the result.
Below is the implementation of the above approach:
Program 1:
// Java program to check if a key exists
// in a HashMap or not
Â
Âimport
java.util.*;
Â
Âpublic
class
GFG {
   Â
public
static
void
main(String[] args)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// Create a HashMap
       Â
HashMap<Integer, String>
           Â
map =
new
HashMap<>();
Â
ÂÂ Â Â Â Â Â Â Â
// Populate the HashMap
       Â
map.put(
1
,
"Geeks"
);
       Â
map.put(
2
,
"ForGeeks"
);
       Â
map.put(
null
,
"GeeksForGeeks"
);
Â
ÂÂ Â Â Â Â Â Â Â
// Get the key to be removed
       Â
int
keyToBeChecked =
2
;
Â
ÂÂ Â Â Â Â Â Â Â
// Print the initial HashMap
       Â
System.out.println(
"HashMap: "
                          Â
+ map);
Â
ÂÂ Â Â Â Â Â Â Â
// Check is key exists in the Map
       Â
boolean
isKeyPresent = map.containsKey(keyToBeChecked);
Â
ÂÂ Â Â Â Â Â Â Â
// Print the result
       Â
System.out.println(
"Does key "
                          Â
+ keyToBeChecked
                          Â
+
" exists: "
                          Â
+ isKeyPresent);
   Â
}
}
Output:HashMap: {null=GeeksForGeeks, 1=Geeks, 2=ForGeeks} Does key 2 exists: true