Java : How to Remove elements from HashMap while Iterating

In this article we will discuss different ways to remove elements from HashMap while Iterating.

Let’s create a HashMap of String as Key and Integer as value and add some elements into it i.e.

// Create a HashMap of string and int
HashMap<String, Integer> wordFreqMap = new HashMap<>();
// Add elements in Map
wordFreqMap.put("this", 6);
wordFreqMap.put("at", 10);
wordFreqMap.put("is", 9);
wordFreqMap.put("for", 1);
wordFreqMap.put("the", 12);

Now lets remove elements from this HashMap while iterating using 2 different techniques

Remove elements from HashMap while Iterating using KeySet

keyset() method of HashMap returns a set of keys in the HashMap and its backed by HashMap i.e. any items removed from the Key Set will be removed from HashMap too.

So lets Iterate over the HashMap using KeySet Iterator and remove all the elements whose value is ODD, while Iterating i.e.

// Create a Iterator to KeySet of HashMap
Iterator<String> it = wordFreqMap.keySet().iterator();

// Iterate over all the elements
while (it.hasNext()) {
	String key = it.next();
	// Check if Value associated with Key is ODD
	if (wordFreqMap.get(key) % 2 == 1) {
		// Remove the element
		it.remove();
	}
}

Remove elements from Hahsmap while Iterating using EntrySet

HashMap member function entrySet() returns a set of Entry<K,V> in the HashMap and its backed by HashMap i.e. any items remover from the Entry Set will be removed from HashMap too.

So lets Iterate over the HashMap using Entry Set Iterator and remove all the elements whose value is 10 while Iterating i.e.

// Create a Iterator to EntrySet of HashMap
Iterator<Entry<String, Integer>> entryIt = wordFreqMap.entrySet().iterator();

// Iterate over all the elements
while (entryIt.hasNext()) {
	Entry<String, Integer> entry = entryIt.next();
	// Check if Value associated with Key is 10
	if (entry.getValue() == 10) {
		// Remove the element
		entryIt.remove();
	}
}

Complete example is as follows,

package com.thispointer.examples.java.collection.hashmap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.function.Predicate;
import java.util.Set;

public class IterateRemove {

	public static void main(String[] args) {
		// Create a HashMap of string and int
		HashMap<String, Integer> wordFreqMap = new HashMap<>();

		// Add elements in Map
		wordFreqMap.put("this", 6);
		wordFreqMap.put("at", 10);
		wordFreqMap.put("is", 9);
		wordFreqMap.put("for", 1);
		wordFreqMap.put("the", 12);

		System.out.println("*********Map Contents ****");

		System.out.println(wordFreqMap);

		System.out.println("Remove While Iterating using KeySet Iterator");
		System.out.println("*********Remove All Elements while Iterating Whose Value is ODD ****");

		// Create a Iterator to KeySet of HashMap
		Iterator<String> it = wordFreqMap.keySet().iterator();
		
		// Iterate over all the elements
		while (it.hasNext()) {
			String key = it.next();
			// Check if Value associated with Key is ODD
			if (wordFreqMap.get(key) % 2 == 1) {
				// Remove the element
				it.remove();
			}
		}

		System.out.println(wordFreqMap);

		System.out.println("Remove While Iterating using EntrySet Iterator");
		System.out.println("*********Remove All Elements Whose Value is 10 ****");

		// Create a Iterator to EntrySet of HashMap
		Iterator<Entry<String, Integer>> entryIt = wordFreqMap.entrySet().iterator();
		
		// Iterate over all the elements
		while (entryIt.hasNext()) {
			Entry<String, Integer> entry = entryIt.next();
			// Check if Value associated with Key is 10
			if (entry.getValue() == 10) {
				// Remove the element
				entryIt.remove();
			}
		}

		System.out.println(wordFreqMap);

	}

}

Output

*********Map Contents ****
{the=12, at=10, this=6, for=1, is=9}
Remove While Iterating using KeySet Iterator
*********Remove All Elements while Iterating Whose Value is ODD ****
{the=12, at=10, this=6}
Remove While Iterating using EntrySet Iterator
*********Remove All Elements Whose Value is 10 ****
{the=12, this=6}

HashSet

Java Interview Questions

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top