Remove empty strings from a List of strings in Java
This post will discuss how to remove empty strings from a List of strings in Java.
1. Using List.removeAll() method
The removeAll() method is commonly used to remove all the elements from the list that are contained in the specified collection. For example, the following code removes all strings that are null or empty from a list of strings:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> collection = new ArrayList<>( Arrays.asList("A", null, "B", null, "C", "", "D")); collection.removeAll(Arrays.asList("", null)); System.out.println(collection); } } |
Output:
[A, B, C, D]
2. Using List.removeIf() method
Java 8 introduced several enhancements to the Collection interface, like the removeIf() method, which removes all items from the list satisfying the given predicate.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; class Main { public static void main(String[] args) { List<String> collection = new ArrayList<>(Arrays.asList("A", null, "B", null, "C", "", "D")); collection.removeIf(Objects::isNull); collection.removeIf(String::isEmpty); System.out.println(collection); } } |
Output:
[A, B, C, D]
The above solution makes two calls to the removeIf() method for removing nulls and empty values. You can use Apache Commons Lang StringUtils.isEmpty() method instead, which checks a string is an empty string or null.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> collection = new ArrayList<>( Arrays.asList("A", null, "B", null, "C", "", "D")); collection.removeIf(StringUtils::isEmpty); System.out.println(collection); } } |
Output:
[A, B, C, D]
This can be answered trivially using lambda expressions without any additional libraries.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { public static void main(String[] args) { List<String> collection = new ArrayList<>(Arrays.asList("A", null, "B", null, "C", "", "D")); collection.removeIf(s -> s == null || "".equals(s)); System.out.println(collection); } } |
Output:
[A, B, C, D]
3. Using Stream.filter() method
Here’s a version using Java 8 Streams that filters a collection by applying specified Predicate to each element. If the predicate returns false, it removes the element. Here’s a working example:
|
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.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.function.Predicate; import java.util.stream.Collectors; class Main { public static void main(String[] args) { List<String> collection = new ArrayList<>(Arrays.asList( "A", null, "B", null, "C", "", "D")); List<String> filteredList = collection.stream() .filter(Objects::nonNull) .filter(Predicate.not(String::isEmpty)) .collect(Collectors.toList()); System.out.println(filteredList); } } |
Output:
[A, B, C, D]
4. Using Iterator
Another solution is to iterate over elements of the list and remove if the current element if it is null and equal to an empty string. You should use the iterator’s own remove() method, to avoid ConcurrentModificationException. See this post for more details on this behavior.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { public static void main(String[] args) { List<String> collection = new ArrayList<>( Arrays.asList("A", null, "B", null, "C", "", "D")); Iterator<String> i = collection.iterator(); while (i.hasNext()) { String s = i.next(); if (s == null || s.isEmpty()) { i.remove(); } } System.out.println(collection); } } |
Output:
[A, B, C, D]
That’s all about removing empty strings from a List of strings 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 :)