How to Delete All Pairs of Keys and Values In Hashmap

In this tutorial we will see how to delete all pairs of keys and values in hashmap . We can remove all key and value mapping from HashMap using clear() method or remove() method through iterator on HashMap. We will discuss remove all mappings from HashMap examples .

  • Remove all mappings from HashMap Java Using clear () Method
  • How to Delete all pairs of keys and values in hashmap Using Iterator¬†

Example 1 : Remove all mappings from HashMap Java Using clear () Method

In this example we will see how to to delete all pairs of keys and values in hashmap using clear() method . we will create HashMap with String and Integer mapping and element in map using put method . We will first print HashMap element . Now we call clear() method to delete all pairs of keys and values in hashmap .To verify that all key value mapping are removed or not , we will print HashMap element again . There is empty HashMap now because all key and value mapping are remove through clear() method .

import java.util.HashMap;
import java.util.Map;

public class HashMapClear {

	public static void main(String[] args) {
		
		Map<String,Integer> hashMap = new HashMap<String,Integer>();

		hashMap.put("Java",1);
		hashMap.put("Vogue",2);
		hashMap.put("Welcomes",3);
		hashMap.put("You",4);
		hashMap.put("Map", 5);
		
		System.out.println("Hashap : "+ hashMap);
	
		hashMap.clear();
		System.out.println("After called clear() method ");
		System.out.println("HashMap : "+ hashMap);
		
	}
}

Output:

Hashap : {Java=1, Vogue=2, Map=5, You=4, Welcomes=3}
After called clear() method 
HashMap : {}

Example 2 : How to Delete all pairs of keys and values in hashmap Using Iterator

Now we will remove all mappings from HashMap using Iterator . We will follow below  algorithm

  • We will Iterate over map using Iterator and call remove() method on Iterator In this approach .
  • We get KeySet from HashMap using keySet() method .
  • iterating on keys of Map
  • We are calling remove() method for each iteration .
  • Here , hasNext() is used to check whether there is a next element present inside the collection or not.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapRemoveAll {
	
	public static void main(String[] args) {
		
		Map<String,Integer> hashMap = new HashMap<String,Integer>();

		hashMap.put("Java",1);
		hashMap.put("Vogue",2);
		hashMap.put("Welcomes",3);
		hashMap.put("You",4);
		hashMap.put("Map", 5);
		
		System.out.println("Hashap : "+ hashMap);
	
		//get set view of HashMap
        Set keyset = hashMap.keySet();

        //iterate HashMap and Remove element 
        Iterator itr = keyset.iterator();
        while (itr.hasNext()) {
            itr.next(); 
            itr.remove();
        }
        
		System.out.println("After removing element ");
		System.out.println("HashMap : "+ hashMap);

	}

}

Output :

Hashap : {Java=1, Vogue=2, Map=5, You=4, Welcomes=3}
After removing element 
HashMap : {}

In this tutorial we have learned different methods to delete all pairs of keys and values in hashmap . you can see more java hashmap program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

69 − = 59