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.
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.
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
FluentIterable.filter
Iterables.filter
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.