How to Remove Key Value Pairs or Entries from HashMap [Snippet]

In this article, we will discuss how to remove key-value pair or entry from HashMap. In this article, we will look into two different ways we can remove key-value pair or entry from HashMap.
  1. Using java.util.Map.remove(Object key) Method
  2. Using java.util.Collection.removeIf(Predicate<? super Entry<String, Integer>> filter) Method from Java 8
If you are new to Java 8 then check out top Java 8 tutorials here.

1. Using java.util.Map.remove(Object key) Method

Ths remove() method removes the mapping for a key from this map if it is present (optional operation).
package net.javaguides.examples;

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

/**
 * Demonstrates How to Remove Key Value Pairs or Entries from HashMap [Snippet]
 * @author Ramesh Fadatare
 *
 */
public class RemoveKeyValuePairHashMap {

    public static void main(String[] args) {

        // create two hashmap
        Map < String, Integer > courseMap = new HashMap < String, Integer > ();

        courseMap.put("Java", 1);
        courseMap.put("C", 2);
        courseMap.put("C++", 3);

        System.out.println("Before removal");
        printMap(courseMap);

        // using normal remove method
        courseMap.remove("Java");

        System.out.println("After removal");
        printMap(courseMap);
    }

    private static void printMap(Map < String, Integer > courseMap) {
        for (String s: courseMap.keySet()) {
            System.out.println(s);
        }
    }
}
Output:
Before removal
Java
C++
C
After removal
C++
C

2. java.util.Collection.removeIf(Predicate<? super Entry<String, Integer>> filter) Method from Java 8

The removeIf() method of Collection class which allows you to remove entries based upon some condition or predicate. It takes a lambda expression, which can be used to supply the predicate you want. For example, we can rewrite the above code in Java 8 as shown in the following example:
package net.javaguides.examples;

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

/**
 * Demonstrates How to Remove Key Value Pairs or Entries from HashMap [Snippet]
 * @author Ramesh Fadatare
 *
 */
public class RemoveKeyValuePairHashMap {

    public static void main(String[] args) {

        // create two hashmap
        Map < String, Integer > courseMap = new HashMap < String, Integer > ();

        courseMap.put("Java", 1);
        courseMap.put("C", 2);
        courseMap.put("C++", 3);

        System.out.println("Before removal : using java 8 ");
        printMap(courseMap);

        // using java 8 removeIf method
        courseMap.entrySet().removeIf(e - > e.getKey().equals("Java"));

        System.out.println("After removal : using Java 8");
        printMap(courseMap);

    }

    private static void printMap(Map < String, Integer > courseMap) {
        for (String s: courseMap.keySet()) {
            System.out.println(s);
        }
    }
}
Output:
Before removal : using java 8 
Java
C++
C
After removal : using Java 8
C++
C

Collections Framework Examples

Comments