How to Remove all Mappings from HashMap in Java with Example

In this tutorial, I will be sharing how to remove all the key-value mappings from the HashMap. In the last tutorial, we learned how to remove an entry object or specific mapping from HashMap based on key. There are 3 ways to achieve our goal:

1. Using clear() method [Easiest]

2. Using removeIf() method

3. Removing through iterator

Read Also  :  How to check size of HashMap in Java

Let's dive deep into the topic:

Remove All Mappings from HashMap

1. Using clear() method


Syntax :
 public void clear()


According to Oracle docs, clear() method removes all of the mappings from the map.

Program to Remove all Mappings from HashMap

 import java.util.*;

public class HashMapRemoveAllMapping {
  public static void main(String[] args) {
      
    // Creating a HashMap object
    HashMap<Integer,String> map = new HashMap<>();
    
    // Integer Keys and String Values are added to the HashMap object
    map.put(50000, "Lawyer");
    map.put(100000, "Engineer");
    map.put(200000, "Doctor");
    
    // Showing HashMap object elements
    System.out.println("Original HashMap: "+ map);
    
    // Removing all the mappings from the HashMap object
    map.clear();
    
    // Showing the updated HashMap object
    System.out.println("Updated HashMap: "+ map);
    
  }
}

Output:
Original HashMap: {50000=Lawyer, 100000=Engineer, 200000=Doctor}
Updated HashMap: {}

2. Using removeIf() method


Syntax:
 default boolean removeIf(Predicate<? super E> filter)


According to Oracle docs, removeIf() method returns true if we are able to remove all of the elements of the given collection satisfying the given predicate.

 import java.util.*;

public class HashMapRemoveAllMapping2 {
    
  public static void main(String[] args) {
      
    // Initializing a HashMap object
    HashMap<String,String> map2 = new HashMap<>();
    
    // String Keys and String Values are added to the HashMap object
    map2.put("Profession1", "Doctor");
    map2.put("Profession2", "Engineer");
    map2.put("Profession3", "Lawyer");
    
    // Printing HashMap object elements
    System.out.println("Original HashMap elements are: "+ map2);
    
    // Fetching all the keys of the HashMap object
    Set<String> keys = map2.keySet();
    
    // entrySet() method is used to iterate the Set
    map2.entrySet().removeIf(entry -> keys.contains(entry.getKey()));
    
    // Printing the transformed HashMap object
    System.out.println("Updated HashMap is: " + map2);
  }
}

Output:
Original HashMap elements are: {Profession2=Engineer, Profession3=Lawyer, Profession1=Doctor}
Updated HashMap is: {}

3. Removing through iterator


In this third approach, you have to traverse through the map using iterator and then call the remove() method on the iterator.

 import java.util.*;

public class HashMapRemoveAllMapping3 {
    
  public static void main(String[] args) {
      
    // Creating a HashMap object
    HashMap<String,Integer> map3 = new HashMap<>();
    
    // String Keys and Integer Values are added to the HashMap object
    map3.put("Salary1", 1000);
    map3.put("Salary2", 2000);
    map3.put("Salary3", 3000);
    
    // Displaying HashMap object elements
    System.out.println("Original HashMap: "+ map3);
    
    // Getting all the keys of the HashMap object
    Set<String> keys = map3.keySet();
    
    // Iterating through the HashMap
    Iterator it = keys.iterator();
    while (it.hasNext())
    {
        it.next();
        it.remove();
    }
    
    // Displaying the transformed HashMap object
    System.out.println("Updated HashMap: "+ map3);
  }
}

Output:
Original HashMap: {Salary3=3000, Salary2=2000, Salary1=1000}
Updated HashMap: {}

That's all for today. Please mention in the comments if you have any questions related to how to remove all mappings from HashMap in Java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry