Removing all Mapping From HashMap in Java
Last Updated :
17 Dec, 2020
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 :
- clear() method
- remove through iterator
- 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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
HashMap<String, Integer> gfg = new HashMap<>();
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());
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
HashMap<String, Integer> gfg = new HashMap<>();
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());
Set keyset = gfg.keySet();
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
import java.io.*;
import java.util.*;
public class GFG {
public static void main(String args[])
{
HashMap<String, Integer> gfg = new HashMap<>();
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());
Set<String> keySet = gfg.keySet();
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...