Filter null values from a Map in Java 8 and above
This post will discuss how to filter null values from a map using streams in Java.
We have discussed how to filter null values from a map in Java using plain Java, Guava library, and Apache Commons Collections in the previous post. This post will discuss how to filter null values from a map using streams in Java 8 and above.
1. Using Collection.removeIf() method
Java 8 introduced several enhancements to the Collection interface, like the removeIf() method. It removes all mappings from the map that satisfy the given predicate.
To filter null values from the map, we can pass Objects.nonNull() to removeIf() method, as shown below:
|
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.HashMap; import java.util.Map; import java.util.Objects; class Main { 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 Map.values() + removeIf() hashMap.values().removeIf(Objects::isNull); System.out.println(hashMap); } } |
Output:
{RED=#FF0000, BLUE=#0000FF, GREEN=#008000}
There are many other ways to filter null values from a map using the removeIf() method:
|
1 2 |
// using lambda expression map.values().removeIf(x -> !Objects.nonNull(x)); |
|
1 2 |
// using Map.entrySet() + removeIf() map.entrySet().removeIf(entry -> entry.getValue() == null); |
|
1 2 |
// using Map.keySet() + removeIf() map.keySet().removeIf(key -> map.get(key) == null); |
2. Using Java 8
We know that Stream.filter() returns a stream consisting of the elements that match the given predicate. We can use a lambda expression to filter null values from the stream of mappings, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; class Main { 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 = hashMap.entrySet() .stream() .filter(entry -> entry.getValue() != null) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); System.out.println(hashMap); } } |
Output:
{RED=#FF0000, BLUE=#0000FF, GREEN=#008000}
This is equivalent to:
|
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 |
import java.util.HashMap; import java.util.Map; class Main { 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); Map<String, String> map = new HashMap<>(); for (Map.Entry<String, String> entry : hashMap.entrySet()) { if (entry.getValue() != null) { if (map.put(entry.getKey(), entry.getValue()) != null) { throw new IllegalStateException("Duplicate key"); } } } hashMap = map; System.out.println(hashMap); } } |
Output:
{RED=#FF0000, BLUE=#0000FF, GREEN=#008000}
3. Handle null map
All the above codes will throw a NullPointerException if the map is null. We can avoid that by creating an empty map if the map is null using Optional.ofNullable(), as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; class Main { public static void main(String[] args) { Map<String, String> hashMap = null; hashMap = Optional.ofNullable(hashMap) .orElseGet(Collections::emptyMap) .entrySet().stream() .filter(entry -> entry.getValue() != null) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); System.out.println(hashMap); // {} } } |
4. Map the null values to a default value
Instead of removing mappings having null values from the map, we can replace the null values with any custom value. To illustrate, the following example replaces null values with a string.
|
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 |
import java.util.*; import java.util.stream.Collectors; class Main { // using streams in Java 8 and above 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 = hashMap.entrySet() .stream() .map(entry -> { if (entry.getValue() == null) { entry.setValue("#######"); // set the default value } return entry; }) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); System.out.println(hashMap); } } |
Output:
{RED=#FF0000, WHITE=#######, BLUE=#0000FF, BLACK=#######, GREEN=#008000}
That’s all about filtering null values from a Map in Java.
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 :)