Java tutorial
//package com.java2s; import java.util.Collection; import java.util.Objects; import java.util.function.Consumer; public class Main { /** * Applies the given consumer to each item in the given collection after filtering * out null items. * * @param collection the collection. * @param consumer the consumer. */ public static <E> void nullSafeForEach(Collection<E> collection, Consumer<E> consumer) { collection.stream().filter(Objects::nonNull).forEach(consumer); } }