DEV Community

Discussion on: Filter Null Values from a List with Java8 Lambda

Collapse
 
roccoprobe profile image
rocco probe

Yup, .filter(Objects::nonNull) is definitely sweet, but one must ask... Why store nulls in a collection in the first place? :)

Collapse
 
monknomo profile image
Gunnar Gissel

You have to go to war with the collection you have; or at least, the collections you are given.

Besides, in some contexts, a null at some collection position has a meaning that it lacks in other contexts.

Let's see if I can come up with a concrete example...

In the context of a revolver, I can imagine a collection where nulls are important - you want to know chamber 5 is empty. But in the context of knowing something about the collection bullets in the revolver, we can ignore the nulls.

There are also Swing UI things - JList, JComboBox, etc. where nulls in collections are useful, but I expect the Swing developer is a marginal case these days.