
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: January 5, 2024
This quick tutorial is going to show how to remove all null elements from a List, using plain Java, Guava, the Apache Commons Collections and the newer Java 8 lambda support.
This article is part of the “Java – Back to Basic” series here on Baeldung.
The Java Collections Framework offers a simple solution for removing all null elements in the List – a basic while loop:
@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null);
while (list.remove(null));
assertThat(list, hasSize(1));
}
Alternatively, we can also use the following simple approach:
@Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJavaAlternative_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null);
list.removeAll(Collections.singleton(null));
assertThat(list, hasSize(1));
}
Note that both these solutions will modify the source list.
We can also remove nulls using Guava and a more functional approach, via predicates:
@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null);
Iterables.removeIf(list, Predicates.isNull());
assertThat(list, hasSize(1));
}
Alternatively, if we don’t want to modify the source list, Guava will allow us to create a new, filter list:
@Test
public void givenListContainsNulls_whenRemovingNullsWithGuavaV2_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, null, 2, 3);
List<Integer> listWithoutNulls = Lists.newArrayList(
Iterables.filter(list, Predicates.notNull()));
assertThat(listWithoutNulls, hasSize(3));
}
Let’s now look at a simple solution using the Apache Commons Collections library using a similar functional style:
@Test
public void givenListContainsNulls_whenRemovingNullsWithCommonsCollections_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
CollectionUtils.filter(list, PredicateUtils.notNullPredicate());
assertThat(list, hasSize(3));
}
Note that this solution will also modify the original list.
Finally – let’s look at a Java 8 solution using Lambdas to filter the List; the filtering process can be done in parallel or serial:
@Test
public void givenListContainsNulls_whenFilteringParallel_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
List<Integer> listWithoutNulls = list.parallelStream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
@Test
public void givenListContainsNulls_whenFilteringSerial_thenCorrect() {
List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null);
List<Integer> listWithoutNulls = list.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() {
List<Integer> listWithoutNulls = Lists.newArrayList(null, 1, 2, null, 3, null);
listWithoutNulls.removeIf(Objects::isNull);
assertThat(listWithoutNulls, hasSize(3));
}
And that’s it – some quick and very useful solutions for getting rid of all null elements from a List.
In this article, we were able to explore the different approaches we can have to remove nulls from a List using Java, Guava or Lambdas.