Site icon

Remove elements/string objects from HashMap collection by key (Java/example)

  1. HashMap is Hash table based implementation of the Map interface.
  2. HashMap provides constant-time performance for the basic operations like get & put.
  3. HashMap based implementation is not thread safe.
    • In multi-threading environment, HashMap should be synchronized externally.
  4. If multiple threads access a hashmap concurrently then HashMap must synchronized externally.

1. Methods to remove element or objects from HashMap collection in java

No.Method Name Description
1V remove(Object key) Removes the mapping for the specified key from this map if present.
2boolean remove(Object key, Object value) Removes the entry for the specified key only if it is currently mapped to the specified value.

We will demonstrate the removal of keys using entrySet iterator.

2. Remove elements/string objects from HashMap collection (java/example)

package org.learn.collection.map.hmap;

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

public class DemoRemoveHashMap {

	public static void main(String[] args) {
		Map<String, String> mapSportsPersonality = new HashMap<>();
		mapSportsPersonality.put("Tennis", "Federer");
		mapSportsPersonality.put("Cricket", "Bradman");
		mapSportsPersonality.put("Basketball", "Jordan");
		mapSportsPersonality.put("Golf", "Woods");
		mapSportsPersonality.put("Boxer", "Ali");

		System.out.println("Demo HashMap collection remove methods : ");
		demoRemoveMethod(mapSportsPersonality);
	}

	private static void demoRemoveMethod(Map<String, String> mapSportsPersonality) {
		// {Tennis=Federer, Cricket=Bradman, Golf=Woods, Basketball=Jordan, Boxer=Ali}
		System.out.println("1. Orignal hashmap:\n" + mapSportsPersonality);

		// Remove Cricket from map
		mapSportsPersonality.remove("Cricket");
		// {Tennis=Federer, Golf=Woods, Basketball=Jordan, Boxer=Ali}
		System.out.println("2. Removed Cricket from map : \n" + mapSportsPersonality);

		mapSportsPersonality.remove("Boxer","Ali");
		// {Tennis=Federer, Golf=Woods, Basketball=Jordan}
		System.out.println("3. Removed entry from map with Key=Boxer and Value=Ali:\n " + mapSportsPersonality);

		Set<Entry<String, String>> entrySet = mapSportsPersonality.entrySet();
		Iterator<Entry<String, String>> iterator = entrySet.iterator();
		while(iterator.hasNext()) {
			Map.Entry<String, String> entry = iterator.next();
			if (entry.getKey().equals("Basketball")) {
				iterator.remove();
			} else if (entry.getKey().equals("Golf")) {
				iterator.remove();
			}
		}
		// {Tennis=Federer}
		System.out.println("4. Removed Basketball,Golf during iteration:\n " + mapSportsPersonality);
	}
}

3. Remove elements/string objects from HashMap collection (java/example)

Demo HashMap collection remove methods: 
1. Orignal hashmap:
{Tennis=Federer, Cricket=Bradman, Golf=Woods, Basketball=Jordan, Boxer=Ali}
2. Removed Cricket from map : 
{Tennis=Federer, Golf=Woods, Basketball=Jordan, Boxer=Ali}
3. Removed entry from map with Key=Boxer and Value=Ali:
 {Tennis=Federer, Golf=Woods, Basketball=Jordan}
4. Removed Basketball,Golf during iteration:
 {Tennis=Federer}
Exit mobile version