Remove nulls from a List in Java
This post will discuss how to remove nulls from a list in Java using plain Java, Guava library, and Apache Commons Collections.
1. Using List.remove() method
List.remove(Object) removes the first occurrence of the specified object from the list. It returns true if the object is removed from the list and returns false if it is not present in the list.
To remove all null occurrences from the list, we can continuously call remove(null) until all null values are removed. Please note that the list will remain unchanged if it does not contain any null value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { // Program to remove nulls from a list in Java public static void main(String[] args) { List<String> colors = new ArrayList<>( Arrays.asList("RED", null, "BLUE", null, "GREEN")); // Plain Java – Using `List.remove()` method while (colors.remove(null)) {} System.out.println(colors); } } |
Output:
[RED, BLUE, GREEN]
2. Using List.removeAll() method
List.removeAll(Collection) removes elements contained in the specified collection from the list.
Unlike the remove() method, removeAll() will throw a NullPointerException if the specified collection is null. To remove all nulls occurrences from the list, 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 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; class Main { // Program to remove nulls from a list in Java public static void main(String[] args) { List<String> colors = new ArrayList<>( Arrays.asList("RED", null, "BLUE", null, "GREEN")); colors.removeAll(Collections.singletonList(null)); System.out.println(colors); } } |
Output:
[RED, BLUE, GREEN]
3. Using Iterator
The idea is very simple – loop through the list using an iterator and remove all null elements from it.
|
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 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { // Generic method to remove nulls from a list in Java public static<T> void removeNulls(List<T> list) { Iterator<T> itr = list.iterator(); while (itr.hasNext()) { T curr = itr.next(); if (curr == null) { itr.remove(); // remove nulls } } } // Program to remove all null values from a list in Java public static void main(String[] args) { List<String> colors = new ArrayList<>( Arrays.asList("RED", null, "BLUE", null, "GREEN")); removeNulls(colors); System.out.println(colors); } } |
Output:
[RED, BLUE, GREEN]
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 |
import com.google.common.base.Predicates; import com.google.common.collect.Iterables; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { // Program to remove nulls from a list in Java using Guava public static void main(String[] args) { List<String> colors = new ArrayList<>( Arrays.asList("RED", null, "BLUE", null, "GREEN")); // using Guava Iterables.removeIf(colors, Predicates.isNull()); System.out.println(colors); } } |
Output:
[RED, BLUE, GREEN]
We can also use lambda expressions in Java 8 and above:
|
1 |
Iterables.removeIf(colors, 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 |
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.PredicateUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; class Main { // Program to remove nulls from a list using Apache Commons Collections public static void main(String[] args) { List<String> colors = new ArrayList<>( Arrays.asList("RED", null, "BLUE", null, "GREEN")); // Using Apache Commons Collections CollectionUtils.filter(colors, PredicateUtils.notNullPredicate()); System.out.println(colors); } } |
Output:
[RED, BLUE, GREEN]
We can also use lambda expressions in Java 8 and above:
|
1 |
CollectionUtils.filter(colors, 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(colors, PredicateUtils.nullPredicate()); |
|
1 |
CollectionUtils.filterInverse(colors, x -> x == null); |
That’s all about removing nulls from a List 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 :)