Filter null, empty, and blank values from a list in Java
This post will discuss how to filter null, empty, and blank values from a list in Java.
1. Using Plain Java
In plain Java, you can use Stream API to filter null, empty, and blank values from a list.
The Stream API provides the filter() method to retain elements that match the specified predicate. In order to remove null, empty, and blank values from a list, you can use the inverse predicate Predicate.not() starting Java 11 and lambda expressions before Java 11.
The following program demonstrates the working of the filter() method to remove null, empty, and blank values. Note that the solution creates a copy of the original list.
|
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 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.function.Predicate; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<String> input = Arrays.asList("A", "B", null, "", "C", "D", null, "", "E", " "); // remove null only List<String> filtered = input.stream() .filter(Objects::nonNull) .collect(Collectors.toList()); System.out.println(filtered); // [A, B, , C, D, , E, ] // remove null and empty values (before Java 11) filtered = input.stream() .filter(s -> s != null && !s.isEmpty()) .collect(Collectors.toList()); System.out.println(filtered); // [A, B, C, D, E, ] // remove null and empty values (After Java 11) filtered = input.stream() .filter(Objects::nonNull) .filter(Predicate.not(String::isEmpty)) .collect(Collectors.toList()); System.out.println(filtered); // [A, B, C, D, E, ] // remove null, empty, and blank values (Before Java 11) filtered = input.stream() .filter(s -> s != null && !s.isBlank()) .collect(Collectors.toList()); System.out.println(filtered); // [A, B, C, D, E] // remove null, empty, and blank values (After Java 11) filtered = input.stream() .filter(Objects::nonNull) .filter(Predicate.not(String::isBlank)) .collect(Collectors.toList()); System.out.println(filtered); // [A, B, C, D, E] } } |
2. Using Apache Commons Lang
If your project uses Apache Commons Lang, you can simply use isNotEmpty() or isNotBlank() method from the StringUtils class, which are null-safe. The following program demonstrates the working of these methods. Note that the solution creates a copy of the original list.
|
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 org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { List<String> input = Arrays.asList("A", "B", null, "", "C", "D", null, "", "E", " "); // remove null and empty values List<String> filtered = input.stream() .filter(StringUtils::isNotEmpty) .collect(Collectors.toList()); System.out.println(filtered); // [A, B, C, D, E, ] // remove null, empty, and blank values filtered = input.stream() .filter(StringUtils::isNotBlank) .collect(Collectors.toList()); System.out.println(filtered); // [A, B, C, D, E] } } |
Output:
[A, B, C, D, E, ]
[A, B, C, D, E]
3. Using removeIf() method
Both above solutions does not modify the original list, but creates a copy of the original list. If you want to modify the list inplace, you can use the removeIf() method. The removeIf() method removes all elements that satisfy the specified predicate. This is demonstrated 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.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; public class Main { public static void main(String[] args) { List<String> input = new ArrayList<>(Arrays.asList("A", "B", null, "", "C", "D", null, "", "E", " ")); // remove null values input.removeIf(Objects::isNull); System.out.println(input); // [A, B, , C, D, , E, ] // remove empty values input.removeIf(String::isEmpty); System.out.println(input); // [A, B, C, D, E, ] // remove blank values input.removeIf(String::isBlank); System.out.println(input); // [A, B, C, D, E] } } |
Output:
[A, B, , C, D, , E, ]
[A, B, C, D, E, ]
[A, B, C, D, E]
That’s all about filtering null, empty, and blank 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 :)