Here is examples Java Remove Element from Map using Map remove element using removeIf, Map remove element using iterator, Map remove element using Stream (Java 8). In java, Map is collection of key and value pair. Here is example of remove element from map using different different ways, as per your requirement and convincing best ways to select using java remove element from map.

Example 1: Map remove element using removeIf (Java 8)

We need to perfrom operation on entrySet here because The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified
while an iteration over the set is in progress (except through the iterator’s own operation, or through the operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map

java.util.Map<String, Integer> languages = new HashMap<>();
        languages.put("Java", 1995);
        languages.put("C", 1980);
        languages.put("PHP", 2000);

        languages.entrySet().removeIf(language->{               // removeIf method return true or false based on that element will be removed.
            return language.getKey().equals("Java");            // Its will return true if element key is Java
        });

        languages.forEach((language,year)->{
            System.out.println(new StringJoiner(" : ").add(language).add(year.toString()));
        });
Output:
C : 1980
PHP : 2000

Example 2: Map remove element using iterator

Map<String, Integer> languages = new HashMap<>();
        languages.put("Java", 1995);
        languages.put("C", 1980);
        languages.put("PHP", 2000);

        Iterator<java.util.Map.Entry<String,Integer>> entryIterator= languages.entrySet().iterator();
        while (entryIterator.hasNext()){
            if(entryIterator.next().getKey().equalsIgnoreCase("C")){      
                entryIterator.remove();                                 // it will remove element from map
            }
        }

        languages.forEach((language,year)->{
            System.out.println(new StringJoiner(" : ").add(language).add(year.toString()));
        });
Output
Java : 1995
PHP : 2000

Example 3: Map remove element using Stream (Java 8)

When we are using Stream and collection Map at that it will create another Map. Its will not remove element from same Collection.

Map<String, Integer> languages = new HashMap<>();
       languages.put("Java", 1995);
       languages.put("C", 1980);
       languages.put("PHP", 2000);

       Map<String,Integer> newLanguages = languages.entrySet()
                .stream().filter(language -> !language.getKey().equalsIgnoreCase("C"))
                .collect(Collectors.toMap(language -> language.getKey(), language -> language.getValue()));

       newLanguages.forEach((language, year) -> {
           System.out.println(new StringJoiner(" : ").add(language).add(year.toString()));
       });
Output:
Java : 1995
PHP : 2000

References:

https://stackoverflow.com/questions/1884889/iterating-over-and-removing-from-a-map

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

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