If you’re working with Spring, check out "REST With Spring":

>> CHECK OUT THE COURSE

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.

1. Remove nulls from a List Using Plain Java

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.

2. Remove nulls from a List Using Google Guava

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));
}

3. Remove nulls from a List Using Apache Commons Collections

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.

4. Remove nulls from a List Using Lambdas (Java 8)

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.

5. Conclusion

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.

The implementation of all of these examples and snippets can be found in the GitHub project. This is a Maven-based project so it should be easy to import and run.

The new Certification Class of "REST With Spring" is finally out:

>> CHECK OUT THE COURSE

Sort by:   newest | oldest | most voted
Maksim Kanev
Guest

Hey Eugen,
I think you have a mistake in example with Commons Collections: it will NOT create a new List. It will change the original because it uses Iterator.remove().

Eugen Paraschiv
Guest

Nice catch Maksim – copy paste mistake – thanks for pointing it out. It’s fixed now – cheers,
Eugen.

ivank2139
Guest

I found a another way to do it I thought was clever, what about this one?

/**
* @param
* @param listWithNulls
* list with nulls
* @return a new list without nulls.
*/
public static List removeNulls(final List listWithNulls) {
final List newList = listWithNulls;
newList.removeAll(Collections.singleton(null));
return newList;
}

Eugen Paraschiv
Guest

Nice one-liner – thanks – I’m including it in the article. Cheers,
Eugen.

Eugen Paraschiv
Guest

That’s cool, but I think it’s already part of the article (under section 1). I thought it wasn’t, but it was in there, presented as an alternative. Thanks anyways for the cool suggestion – it’s very clean and to the point. Cheers,
Eugen.

Marco Baumeler
Guest

In Java 8, if you want to mutate your collection, you can also use the removeIf method, so you don’t have to build a stream:

@Test
public void givenListContainsNulls_whenRemovingNullsWithRemoveIf_thenCorrect() {
final List list = Lists.newArrayList(null, 1, 2, null, 3, null);
list.removeIf(p -> p == null);
assertEquals(3, list.size());
}

Eugen Paraschiv
Guest

Nice, didn’t know about that one. I’ll integrate it in the article. Cheers,
Eugen.

gopu0000
Guest

create POJO file then add @JsonInclude, its will eliminate Null value

import com.fasterxml.jackson.annotation.JsonInclude;

@JsonInclude(JsonInclude.Include.NON_EMPTY)

public class UserEducation {

private long id;

private int user_id;

Eugen Paraschiv
Guest

Hey Gopu – that’s suggestion assumes you’re using Jackson – which is not the case here. So unfortunately that won’t quite work out. Cheers,
Eugen.

Niranjan Nanda
Guest

I think in Java 8 you can use:

final List newList = currentList.stream().filter(Objects::nonNull).collect(Collectors.toList());

Grzegorz Piwowarek
Guest

Good idea. Keep in mind that the article is almost 3 years old 🙂 We will update it. Thanks!

Niranjan Nanda
Guest

I posted it because I saw some Java 8 examples. But it’s okay; I didn’t realize its that old. Anyways, you can remove my comment from this if it’s confusing. 🙂

Grzegorz Piwowarek
Guest

if you look closely, you can see that we updated the article using the method references 🙂

wpDiscuz