Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Remove an Entry using key from HashMap while Iterating over it

  • Difficulty Level : Easy
  • Last Updated : 11 Dec, 2018

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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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:
    1. Get the HashMap and the Key
    2. Create an iterator to iterate over the HashMap using HashMap.iterate() method.
    3. Iterate over the HashMap using the Iterator.hasNext() method.
    4. 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.
    5. If the key matches, remove the entry of that iteration from the HashMap using remove() method.
    6. 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:
    1. Get the HashMap and the Key
    2. Get the entry set of this map using HashMap.entrySet() method.
    3. 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.
    4. 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}
    



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!