Saturday, September 6, 2025
HomeLanguagesJavaRemoving all Mapping From HashMap in Java

Removing all Mapping From HashMap in Java

Maps are used for when you want to associate a key with a value and Lists are an ordered collection. Map is an interface in the Java Collection Framework and a HashMap is one implementation of the Map interface. HashMap is efficient for locating a value based on a key and inserting and deleting values based on a key.

For removing all mappings from a HashMap in java we can use various approaches :

  1. clear() method
  2. remove through iterator
  3. removeIf() method

Method 1: Using clear() method

The java.util.HashMap.clear() method in Java is used to clear and remove all the elements or mappings from a specified HashMap.

Syntax:

Hash_Map.clear()

Java




// Java program to remove mappings from
// HashMap using clear() method
  
import java.io.*;
  
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
  
        HashMap<String, Integer> gfg = new HashMap<>();
  
        // adding values in hashMap 1
        gfg.put("DSA", 100);
        gfg.put("Problem Solving", 100);
        gfg.put("Development", 99);
        gfg.put("Interviews", 99);
        gfg.put("Competitive Programming", 97);
        gfg.put("FANG", 99);
  
        // printing the size and elements
        System.out.println("-------before removing------");
        System.out.println(gfg);
        System.out.println(gfg.size());
  
        // clear() method
        gfg.clear();
  
        System.out.println("--------After removing-------");
        System.out.println(gfg);
        System.out.println(gfg.size());
    }
}


Output

-------before removing------
{DSA=100, FANG=99, Competitive Programming=97, Problem Solving=100, Development=99, Interviews=99}
6
--------After removing-------
{}
0

Method 2: remove through the iterator

  • In this approach, you will iterate over the Map using iterator and then call remove on the iterator.
  • In this case, we are iterating over keys obtained from the calling keySet() method.
  • hasNext() is used to check whether there is a next element present inside the collection or not.

Java




// Java program to remove each key value pair
// by iterating over the Hashmap
  
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        HashMap<String, Integer> gfg = new HashMap<>();
  
        // adding values in hashMap 1
        gfg.put("DSA", 100);
        gfg.put("Problem Solving", 100);
        gfg.put("Development", 99);
        gfg.put("Interviews", 99);
        gfg.put("Competitive Programming", 97);
        gfg.put("FANG", 99);
  
        // printing the size and elements
        System.out.println("-------before removing------");
        System.out.println(gfg);
        System.out.println(gfg.size());
  
        // getting all keys of map using keySet()
  
        Set keyset = gfg.keySet();
  
        // iterating over the keys and removing
        // hasNext() method is used to check whether next
        // element present inside the collection or not
  
        Iterator itr = keyset.iterator();
        while (itr.hasNext()) {
            itr.next();
            itr.remove();
        }
  
        System.out.println("--------After removing-------");
        System.out.println(gfg);
        System.out.println(gfg.size());
    }
}


Output

-------before removing------
{DSA=100, FANG=99, Competitive Programming=97, Problem Solving=100, Development=99, Interviews=99}
6
--------After removing-------
{}
0

Method 3: Using removeIf() method

Syntax:

public boolean removeIf(Predicate filter)

Parameter: This method takes a parameter filter which represents a predicate which returns true for elements to be removed.

Returns: This method returns True if the predicate returns true, and we are able to remove elements.

Exception: This method throws NullPointerException if the specified filter is null.

  • In this method, you need to specify the condition on which the element will be removed but for our case, we need to remove all elements.
  • Hence, we need to iterate on every key in entryset. We will check the set of keys obtained using the keySet() method.
  • We are removing every key of entrySet which is present in keySet. Every entry object, 1 entry=1key+1 value.
  • keySet() gives all keys present inside the Map.

Java




// Java program to remove the hashings
// from HashMap using removeIf()
  
import java.io.*;
  
import java.util.*;
  
public class GFG {
    public static void main(String args[])
    {
  
        HashMap<String, Integer> gfg = new HashMap<>();
  
        // adding values in hashMap 1
        gfg.put("DSA", 100);
        gfg.put("Problem Solving", 100);
        gfg.put("Development", 99);
        gfg.put("Interviews", 99);
        gfg.put("Competitive Programming", 97);
        gfg.put("FANG", 99);
  
        System.out.println("-------before removing------");
        System.out.println(gfg);
        System.out.println(gfg.size());
  
        // getting all the keys of map
  
        Set<String> keySet = gfg.keySet();
  
        // checking the entry set 
        // key in keySet and remove
        // it one by one
        gfg.entrySet().removeIf(
            entry -> keySet.contains(entry.getKey()));
  
        System.out.println("--------After removing-------");
        System.out.println(gfg);
        System.out.println(gfg.size());
    }
}


Output

-------before removing------
{DSA=100, FANG=99, Competitive Programming=97, Problem Solving=100, Development=99, Interviews=99}
6
--------After removing-------
{}
0
RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6640 POSTS0 COMMENTS
Nicole Veronica
11806 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6754 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS