Remove null values from a List in Java 8 and above
This post will discuss how to remove null values from a list using streams in Java.
We have discussed how to remove null values from a list in Java using plain Java, Guava library, and Apache Commons Collections in the previous post. This post will discuss how to remove nulls from the list using streams in Java 8 and above.
1. Using Collection.removeIf() method
Java 8 introduced several enhancements to Collection interface, like removeIf() method. It removes all elements of the list that satisfy the given predicate.
To remove null values from a list, we can pass Objects.nonNull() to removeIf() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.List; import java.util.ArrayList; import java.util.Objects; class Main { public static void main(String[] args) { List<String> colors = new ArrayList<>(Arrays.asList("RED", null, "BLUE", null, "GREEN")); // using removeIf() + Objects.isNull() colors.removeIf(Objects::isNull); System.out.println(colors); } } |
Output:
[RED, BLUE, GREEN]
There are many other ways to remove null values from a list using the removeIf() method, as shown below:
|
1 |
colors.removeIf(x -> !Objects.nonNull(x)); |
|
1 |
colors.removeIf(x -> x == null); |
2. Using Java 8
We can use the Stream.filter() method that returns a stream consisting of the elements that match the given predicate. We can specify a lambda expression or method reference to remove null values from the stream, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("RED", null, "BLUE", null, "GREEN"); colors = colors.stream() .filter(x -> x != null) // or `Objects::nonNull` .collect(Collectors.toList()); System.out.println(colors); } } |
Output:
[RED, BLUE, GREEN]
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 |
import java.util.Arrays; import java.util.List;import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("RED", null, "BLUE", null, "GREEN"); List<String> list = new ArrayList<>(); for (String color : colors) { if (color != null) { list.add(color); } } colors = list; System.out.println(colors); } } |
Output:
[RED, BLUE, GREEN]
3. Handle null list
Calling stream() and removeIf() methods on a null list will throw a NullPointerException. We can avoid that by creating an empty list (if list 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 |
import java.util.*; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<String> colors = null; colors = Optional.ofNullable(colors) .orElseGet(Collections::emptyList) .stream() .filter(x -> x != null) .collect(Collectors.toList()); System.out.println(colors); } } |
Output:
[]
4. Map the null values to a default value
Instead of removing null values from a list, 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 |
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; class Main { // using streams in Java 8 and above public static void main(String[] args) { List<String> colors = Arrays.asList("RED", null, "BLUE", null, "GREEN"); colors = colors.stream() .map(x -> (x == null) ? "####" : x) .collect(Collectors.toList()); System.out.println(colors); } } |
Output:
[RED, ####, BLUE, ####, GREEN]
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> colors = Arrays.asList("RED", null, "BLUE", null, "GREEN"); List<String> list = new ArrayList<>(); for (String x : colors) { String s = (x == null) ? "####" : x; list.add(s); } colors = list; System.out.println(colors); } } |
Output:
[RED, ####, BLUE, ####, GREEN]
That’s all about removing null values from a List 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 :)