Remove null values from a Map in Java
This post will discuss how to remove null values from a map in Java using plain Java, Guava library, and Apache Commons Collections.
Many operations on the map will throw a NullPointerException if any null values are present in it. There are many options to remove null values from a map:
1. Using Map.remove() method
Collection.remove(Object) removes the first occurrence of the specified object from the collection. It returns true if the object is removed from the collection and returns false if it is not present.
To remove all occurrences of null values from the map, we can get a Collection of values from Map.values() and then continuously call remove(null) until all null values are removed.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.HashMap; import java.util.Map; class Main { // Program to remove null values from a map in Java public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLACK", null); hashMap.put("BLUE", "#0000FF"); hashMap.put("GREEN", "#008000"); hashMap.put("WHITE", null); while (hashMap.values().remove(null)); System.out.println(hashMap); } } |
Output:
{RED=#FF0000, BLUE=#0000FF, GREEN=#008000}
2. Using Map.removeAll() method
Map.values().removeAll(Collection) removes all elements contained in the specified collection from the Collection of values returned by Map.values().
The removeAll() method will throw a NullPointerException if the specified collection is null. To remove all occurrences of null values from the map, we can pass a singleton list or set containing only null.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Collections; import java.util.HashMap; import java.util.Map; class Main { // Program to remove null values from a map in Java public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLACK", null); hashMap.put("BLUE", "#0000FF"); hashMap.put("GREEN", "#008000"); hashMap.put("WHITE", null); hashMap.values().removeAll(Collections.singleton(null)); System.out.println(hashMap); } } |
Output:
{RED=#FF0000, BLUE=#0000FF, GREEN=#008000}
3. Using Iterator
The idea is very simple – loop through the map using an iterator and remove all mappings having null values.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.HashMap; import java.util.Iterator; import java.util.Map; class Main { // Generic method to remove null values from a map in Java public static<K, V> void removeNulls(Map<K, V> map) { Iterator<Map.Entry<K, V>> itr = map.entrySet().iterator(); while (itr.hasNext()) { Map.Entry<K, V> curr = itr.next(); if (curr.getValue() == null) { itr.remove(); } } } // Program to remove null values from a map in Java public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLACK", null); hashMap.put("BLUE", "#0000FF"); hashMap.put("GREEN", "#008000"); hashMap.put("WHITE", null); removeNulls(hashMap); System.out.println(hashMap); } } |
Output:
{RED=#FF0000, BLUE=#0000FF, GREEN=#008000}
4. Using Guava Library
Guava’s Iterables class provides removeIf(Iterable, Predicate) that removes every element from a specified iterable that satisfies the provided predicate.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import com.google.common.base.Predicates; import com.google.common.collect.Iterables; import java.util.HashMap; import java.util.Map; class Main { // Program to remove null values from a map in Java using Guava public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLACK", null); hashMap.put("BLUE", "#0000FF"); hashMap.put("GREEN", "#008000"); hashMap.put("WHITE", null); // using Guava Iterables.removeIf(hashMap.values(), Predicates.isNull()); System.out.println(hashMap); } } |
Output:
{RED=#FF0000, BLUE=#0000FF, GREEN=#008000}
We can also use lambda expressions in Java 8 and above:
|
1 |
Iterables.removeIf(hashMap.values(), x -> x == null); |
5. Using Apache Commons Collections
Apache Commons Collections CollectionUtils class provides filter(Iterable, Predicate) that can filter the collection by applying specified Predicate to each element. If the predicate returns false, it removes the element.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.PredicateUtils; import java.util.HashMap; import java.util.Map; class Main { // Program to remove null values from a map using Apache Commons Collections public static void main(String[] args) { Map<String, String> hashMap = new HashMap<>(); hashMap.put("RED", "#FF0000"); hashMap.put("BLACK", null); hashMap.put("BLUE", "#0000FF"); hashMap.put("GREEN", "#008000"); hashMap.put("WHITE", null); // Using Apache Commons Collections CollectionUtils.filter(hashMap.values(), PredicateUtils.notNullPredicate()); System.out.println(hashMap); } } |
Output:
{RED=#FF0000, BLUE=#0000FF, GREEN=#008000}
We can also use lambda expressions in Java 8 and above:
|
1 |
CollectionUtils.filter(hashMap.values(), x -> x != null); |
Apache Commons Collections also provides the filterInverse(Iterable, Predicate) that works similarly, except it removes the element if the predicate returns true.
|
1 |
CollectionUtils.filterInverse(hashMap.values(), PredicateUtils.nullPredicate()); |
|
1 |
CollectionUtils.filterInverse(hashMap.values(), x -> x == null); |
That’s all about removing null values from a Map in Java.
Suggested Read:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)