This
article
by
Shubhra Srivastava
looks at processing lists containing nulls in Java. Let’s look at doing the same in Groovy.
Shubhra’s article covered both mutating the list in place and producing a new list,
so we’ll cover both cases.
Shubhra’s article examined
"out-of-the-box"
Java and some other collections libraries.
We’ll look at using those same libraries with Groovy, but we’ll cover Eclipse Collections too.
In summary, we’ll cover:
Guava
which provides a number of extensions to the JDK collections ecosystem. In particular, it has immutable collections, new collection types like multisets and bidirectional maps and various powerful extensions and utilities.
Apache Commons Collections
which extends upon the JDK collections framework adding some new types like bidirectional maps and bags
as well as providing many comparator and iterator implementations. The library was designed to fill
gaps in the JDK offerings and while some of those gaps in the JDK have now been filled by the JDK
itself, Commons Collections still contains much useful functionality.
Eclipse Collections
which comes with many container types including
immutable collections, primitive collections, bimaps, multimaps and bags as well as numerous utility
methods. It focuses on reduced memory footprint and efficient containers. It might be particularly
of interest if you need primitive collections, immutable collections or some more exotic collection
types like bag or bidirectional maps.
Groovy
With Groovy, we can follow Java’s lead and use
removeIf
or use Groovy’s
removeAll
.
In either case, we can use a method reference, closure syntax or lambda syntax
to capture the desired non-null constraint.
var list = ['A', null, 'B', null, 'C']
list.removeIf(Objects::isNull)
assert list.size() == 3
var list = ['A', null, 'B', null, 'C']
CollectionUtils.filter(list, PredicateUtils.notNullPredicate())
assert list.size() == 3
In this case, we can also use
findAll
and
grep
no-arg shortcuts.
These shortcuts follow
Groovy truth
which will remove nulls but also empty Strings and zeros. This may or may not be what we want.
assert ['A', null, 'B'].findAll().size() == 2
assert ['A', null, 'B'].grep().size() == 2
assert ['A', null, 'B', '', 0].findAll().size() == 2
assert ['A', null, 'B', '', 0].grep().size() == 2
Groovy also has the
findResults
method which specifically looks
for non-null results rather than applying Groovy truth:
assert ['A', null, 'B'].findResults{ it }.size() == 2
assert ['A', null, 'B'].findResults().size() == 2 // (1)
assert ['A', null, 'B', '', 0].findResults().size() == 4 // (1)
Before concluding, we should mention some other Groovy functionality related to null.
Although not related to list processing, Groovy’s
@NullCheck
AST transform is useful
to automatically add null checking into your own classes, methods, and constructors. For further details,
see the
documentation
.
The Groovy programming language is supported by the
Apache Software Foundation
and the Groovy community.
Apache® and the Apache feather logo are either registered trademarks or trademarks of The Apache Software Foundation.