0
0
JavaHow-ToBeginner · 3 min read

How to Remove Entry from HashMap in Java: Simple Guide

To remove an entry from a HashMap in Java, use the remove(key) method to delete the entry with the specified key. You can also use remove(key, value) to remove the entry only if the key is mapped to the given value.
📐

Syntax

The HashMap class provides two main methods to remove entries:

  • remove(key): Removes the entry with the specified key.
  • remove(key, value): Removes the entry only if the key is mapped to the specified value.

The remove(key) method returns the removed value or null if no mapping was found. The remove(key, value) method returns a boolean indicating whether the entry was removed.

java
hashMap.remove(key);
hashMap.remove(key, value);
💻

Example

This example shows how to create a HashMap, add entries, and remove entries using both remove(key) and remove(key, value) methods.

java
import java.util.HashMap;

public class RemoveEntryExample {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("apple", 10);
        map.put("banana", 20);
        map.put("cherry", 30);

        System.out.println("Original map: " + map);

        // Remove by key
        map.remove("banana");
        System.out.println("After removing 'banana': " + map);

        // Remove by key and value
        boolean removed = map.remove("cherry", 25); // wrong value, won't remove
        System.out.println("Attempt to remove 'cherry' with value 25: " + removed);
        System.out.println("Map now: " + map);

        removed = map.remove("cherry", 30); // correct value, will remove
        System.out.println("Attempt to remove 'cherry' with value 30: " + removed);
        System.out.println("Map now: " + map);
    }
}
Output
Original map: {apple=10, banana=20, cherry=30} After removing 'banana': {apple=10, cherry=30} Attempt to remove 'cherry' with value 25: false Map now: {apple=10, cherry=30} Attempt to remove 'cherry' with value 30: true Map now: {apple=10}
⚠️

Common Pitfalls

Some common mistakes when removing entries from a HashMap include:

  • Trying to remove an entry with a key that does not exist, which returns null or false but does not throw an error.
  • Using remove(key, value) with a wrong value, so the entry is not removed.
  • Modifying the map while iterating over it without using an iterator's remove() method, which causes ConcurrentModificationException.

Always check the return value of remove() to confirm if removal succeeded.

java
import java.util.HashMap;

public class PitfallExample {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("key1", 100);

        // Wrong removal: key does not exist
        Integer removedValue = map.remove("key2");
        System.out.println("Removed value for 'key2': " + removedValue); // prints null

        // Wrong removal: key exists but value does not match
        boolean removed = map.remove("key1", 200);
        System.out.println("Removed 'key1' with value 200: " + removed); // prints false

        // Correct removal
        removed = map.remove("key1", 100);
        System.out.println("Removed 'key1' with value 100: " + removed); // prints true
    }
}
Output
Removed value for 'key2': null Removed 'key1' with value 200: false Removed 'key1' with value 100: true
📊

Quick Reference

Summary tips for removing entries from a HashMap:

  • Use remove(key) to delete by key.
  • Use remove(key, value) to delete only if key maps to value.
  • Check the return value to confirm removal.
  • Avoid modifying the map during iteration without an iterator.

Key Takeaways

Use hashMap.remove(key) to remove an entry by its key.
Use hashMap.remove(key, value) to remove only if the key maps to the given value.
Check the return value of remove() to know if removal succeeded.
Avoid modifying a HashMap while iterating over it without using an iterator.
Removing a non-existent key returns null or false but does not cause errors.