Is there an elegant way to remove nulls while transforming a Collection using Guava?

There’s already a predicate in Predicates that will help you here — Predicates.notNull() — and you can use Iterables.filter() and the fact that Lists.newArrayList() can take an Iterable to clean this up a little more.

Collection<String> resourceIds = Lists.newArrayList(
  Iterables.filter(
     Iterables.transform(matchingComputers, yourFunction),
     Predicates.notNull()
  )
);

If you don’t actually need a Collection, just an Iterable, then the Lists.newArrayList() call can go away too and you’re one step cleaner again!

I suspect you might find that the Function will come in handy again, and will be most useful declared as

public class Computer {
    // ...
    public static Function<Computer, String> TO_ID = ...;
}

which cleans this up even more (and will promote reuse).

Leave a Comment