Given a HashMap and a key in Java, the task is to remove an entry from this HashMap using the key, while iterating over it.
Examples:
Input: HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks}, key = 2 Output: {1=Geeks, 3=GeeksForGeeks} Input: HashMap: {1=G, 2=e, 3=e, 4=k, 5=s}, key = 3 Output: {1=G, 2=e, 4=k, 5=s}
- Using Java 7 and before:
- 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, remove the entry of that iteration from the HashMap using remove() method.
- The required entry has been successfully removed.
Program:
// Java program to remove an entry using key
// from a HashMap while iterating over it
Â
Â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
keyToBeRemoved =
2
;
Â
ÂÂ Â Â Â Â Â Â Â
// Print the initial HashMap
       Â
System.out.println(
"Original HashMap: "
                          Â
+ map);
Â
ÂÂ Â Â Â Â Â Â Â
// Get the iterator over the HashMap
       Â
Iterator<Map.Entry<Integer, String> >
           Â
iterator = map.entrySet().iterator();
Â
ÂÂ Â Â Â Â Â Â Â
// 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
(keyToBeRemoved == entry.getKey()) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
// Remove this entry from HashMap
               Â
iterator.remove();
           Â
}
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
// Print the modified HashMap
       Â
System.out.println(
"Modified HashMap: "
                          Â
+ map);
   Â
}
}
Output:Original HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks} Modified HashMap: {1=Geeks, 3=GeeksForGeeks}
- Using Java 8 lambda expressions:
- Get the HashMap and the Key
- Get the entry set of this map using HashMap.entrySet() method.
- Using lambda expression, remove the entry from the map if the key is equal to the key specified. The entry key of the Map can be obtained with the help of entry.getKey() method.
- The required entry has been successfully removed.
Program:
// Java program to remove an entry using key
// from a HashMap while iterating over it
Â
Â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
keyToBeRemoved =
2
;
Â
ÂÂ Â Â Â Â Â Â Â
// Print the initial HashMap
       Â
System.out.println(
"Original HashMap: "
                          Â
+ map);
Â
ÂÂ Â Â Â Â Â Â Â
// Remove the specified entry from the Map
       Â
map.entrySet()
           Â
.removeIf(
               Â
entry -> (keyToBeRemoved == entry.getKey()));
Â
ÂÂ Â Â Â Â Â Â Â
// Print the modified HashMap
       Â
System.out.println(
"Modified HashMap: "
                          Â
+ map);
   Â
}
}
Output:Original HashMap: {1=Geeks, 2=ForGeeks, 3=GeeksForGeeks} Modified HashMap: {1=Geeks, 3=GeeksForGeeks}