Removing elements from a HashMap in Java

Hashmaps are widely used in Java for storing and retrieving key-value pairs efficiently. Occasionally, there may be a need to remove elements from a HashMap based on certain criteria. In this blog post, we will explore different ways to remove elements from a HashMap in Java.

Overview of the HashMap class

In Java, the HashMap class belongs to the java.util package and provides an implementation of the Map interface. It allows us to store key-value pairs and provides fast retrieval of values based on their keys. HashMaps are not thread-safe by default.

Removing elements by key

To remove an element from a HashMap based on its key, we can use the remove() method. This method takes the key as an argument and returns the value associated with that key. If the key does not exist in the HashMap, it returns null.

Here’s an example code snippet that demonstrates how to remove an element from a HashMap by key:

HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("apple", 10);
hashMap.put("banana", 5);

Integer removedValue = hashMap.remove("apple");
System.out.println("Removed value: " + removedValue);

In the above code, we create a HashMap hashMap and populate it with two key-value pairs. We then remove the element with the key “apple” using the remove() method and store the returned value in the removedValue variable. Finally, we print the removed value which is 10 in this case.

Removing elements using an Iterator

Another way to remove elements from a HashMap is by using an Iterator. This method is useful when we want to remove multiple elements that meet a certain condition.

Here’s an example code snippet that demonstrates how to remove particular elements from a HashMap using an Iterator:

HashMap<String, Integer> hashMap = new HashMap<>();
hashMap.put("apple", 10);
hashMap.put("banana", 5);
hashMap.put("orange", 15);

Iterator<Map.Entry<String, Integer>> iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()) {
    Map.Entry<String, Integer> entry = iterator.next();
    if (entry.getValue() < 10) {
        iterator.remove();
    }
}

In the above code, we create a HashMap hashMap with three key-value pairs. We obtain an Iterator from the entrySet() method of the HashMap. We then iterate over the entries using the hasNext() and next() methods of the Iterator. If the value of a particular entry is less than 10, we remove it using the remove() method of the Iterator.

Conclusion

In this blog post, we have explored two common approaches to removing elements from a HashMap in Java. By key or by using an Iterator, we have seen how to remove elements based on specific conditions. It’s important to note that when removing elements using an Iterator, we need to use the Iterator’s remove() method to avoid ConcurrentModificationException.