Filter null from collection

This examples shows how to remove null elements from an arraylist using straight up java, google guava and apache commons. In a related example find out how to filter null values in groovy.

Straight up Java

Using Lists.removeAll we will remove all null elements from an arrayList.

@Test
public void remove_null_from_list_java () {

    List<String> strings = new ArrayList<>();
    strings.add(null);
    strings.add("www");
    strings.add(null);
    strings.add("leveluplunch");
    strings.add("com");
    strings.add(null);
    
    strings.removeAll(Collections.singleton(null));
    
    assertEquals(3, strings.size());
}

Java 8

Using java 8 we will demonstrate how to remove nulls from an arraylist. In the snippet below we will call the stream api passing a predicate through a lambda expression that will check if an element is null. Or you could use Objects static method nonNull which will return true if the provided reference is non-null.

@Test
public void remove_null_from_list_java8_lambda () {
    
    List<String> strings = Lists.newArrayList(
            null, "www", null, 
            "leveluplunch", "com", null);

    List<String> filterStrings = strings
            .stream()
            .filter(p -> p != null)
            .collect(Collectors.toList());
    
    assertEquals(3, filterStrings.size());

    // or
    List<String> filterStrings2 = strings
            .stream()
            .filter(Objects::nonNull)
            .collect(Collectors.toList());
    
    assertEquals(3, filterStrings2.size());
}

Google Guava

Using guava utilities classes, Iterables, FluentIterable utility class and Collections2 we will call the filter method passing it a guava predicate from the static utility class Predicates. The Predicates.notNull will return true if the object being referenced is not null.

Collections2.filter

@Test
public void remove_null_from_list_guava_collections2 () {

    List<String> strings = Lists.newArrayList(
            null, "www", null, 
            "leveluplunch", "com", null);

    
    Collection<String> filterStrings = Collections2
            .filter(strings, Predicates.notNull());

    assertEquals(3, filterStrings.size());
}

FluentIterable.filter

@Test
public void remove_null_from_list_guava_fluentiterbale () {

    List<String> strings = Lists.newArrayList(
            null, "www", null, 
            "leveluplunch", "com", null);

    
    List<String> filterStrings = FluentIterable.from(strings)
            .filter(Predicates.notNull())
            .toList();

    assertEquals(3, filterStrings.size());
}

Iterables.filter

@Test
public void remove_null_from_list_guava_iterables () {

    List<String> strings = Lists.newArrayList(
            null, "www", null, 
            "leveluplunch", "com", null);

    Iterable<String> filterStrings = Iterables.filter(strings, 
            Predicates.notNull());
    
    assertThat(filterStrings, IsIterableWithSize.<String>iterableWithSize(3));
}

Apache Commons

To filter null from an arraylist using apache commons, we will call CollectionsUtils.filter passing it a predicate that will return true if the object is null.

@Test
public void remove_null_from_list_apache_commons () {
    
    List<String> strings = new ArrayList<>();
    strings.add(null);
    strings.add("www");
    strings.add(null);
    strings.add("leveluplunch");
    strings.add("com");
    strings.add(null);

    
    CollectionUtils.filter(strings, new org.apache.commons.collections.Predicate() {
        public boolean evaluate(Object obj) {
            return obj != null;
        }
    });
    
    assertEquals(3, strings.size());
}