50

What are idiomatic ways of performing a null-check before generating a stream?

I have a method that receives a List that might be null. So I can't just call stream() on the value that is passed. Is there some static helper in that would give me an empty stream if a value is null?

1
  • 2
    I heavily suggest to work on the list providing side. The whole purpose of optionals is to not deal with null references. If a method returns a list that could be null, it should return an optional instead. If that's not possible, you can wrap it into an optional (according to the answers). Additional, you want to do something with that list, i.e. you want to consume it if it is present. So simply call Optional.ifPresent on it. Commented Apr 2, 2015 at 7:09

7 Answers 7

83

I agree with Stuart Marks that list == null ? Stream.empty() : list.stream() is the right way to do this (see his answer), or at least the right way to do this pre-Java 9 (see edit below), but I'll leave this answer up to demonstrate usage of the Optional API.

<T> Stream<T> getStream(List<T> list) {
    return Optional.ofNullable(list).map(List::stream).orElseGet(Stream::empty);
}

Edit: Java 9 added the static factory method Stream.<T>ofNullable(T), which returns the empty stream given a null argument, otherwise a stream with the argument as its only element. If the argument is a collection, we can then flatMap to turn it into a stream.

<T> Stream<T> fromNullableCollection(Collection<? extends T> collection) {
    return Stream.ofNullable(collection).flatMap(Collection::stream);
}

This doesn't misuse the Optional API as discussed by Stuart Marks, and in contrast to the ternary operator solution, there's no opportunity for a null pointer exception (like if you weren't paying attention and screwed up the order of the operands). It also works with an upper-bounded wildcard without needing SuppressWarnings("unchecked") thanks to the signature of flatMap, so you can get a Stream<T> from a collection of elements of any subtype of T.

4
  • I like this example of a helper I could reuse a bunch and avoid having Optional.orNullable littered throughout my code.
    – checketts
    Commented Apr 2, 2015 at 6:59
  • 6
    There is no need for the explicit type parameter <T> in List::stream; it works without. And .orElse(Stream.empty()) may be replaced by .orElseGet(Stream::empty) which is slightly more efficient as it avoids constructing an empty stream.
    – Holger
    Commented Apr 2, 2015 at 10:17
  • @Holger, I made those changes, but I'm wondering what Stream::empty actually produces behind the scenes. An anonymous class?
    – gdejohn
    Commented Apr 2, 2015 at 11:05
  • 4
    See “How will Java lambda functions be compiled?” and “Does a lambda expression create an object on the heap every time it's executed?”. Practically, there will be an instance of an anonymous class but it will be the same instance in every execution of the code (while Stream.empty() returns a new instance each time).
    – Holger
    Commented Apr 2, 2015 at 11:13
30

The best thing I can think of would be to use an Optional with the orElseGet method.

return Optional.ofNullable(userList)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(user -> user.getName())
                .collect(toList());

Updated with @Misha's suggest to use Collections::emptyList over ArrayList::new

2
  • 4
    It's better to use Collections::emptyList instead of ArrayList::new. It makes the intent more clear and it's (slightly) more efficient.
    – Misha
    Commented Apr 2, 2015 at 6:43
  • We can now use .orElseGet(List.of()) from Java 9 onwards
    – pxm
    Commented Aug 3, 2023 at 18:06
18

In the other answers, the Optional instance is created and used strictly within the same statement. The Optional class is primarily useful for communicating with the caller about presence or absence of a return value, fused with the actual value if present. Using it wholly within a single method seems unnecessary.

Let me propose the following more prosaic technique:

static <T> Stream<T> nullableListToStream(List<T> list) {
    return list == null ? Stream.empty() : list.stream();
}

I guess the ternary operator is somewhat déclassé these days, but I think this is the simplest and most efficient of the solutions.

If I were writing this for real (that is, for a real library, not just sample code on Stack Overflow) I'd put in wildcards so that that the stream return type can vary from the List type. Oh, and it can be a Collection, since that's where the stream() method is defined:

@SuppressWarnings("unchecked")
static <T> Stream<T> nullableCollectionToStream(Collection<? extends T> coll) {
    return coll == null ? Stream.empty() : (Stream<T>)coll.stream();
}

(The warning suppression is necessary because of the cast from Stream<? extends T> to Stream<T> which is safe, but the compiler doesn't know that.)

0
13

apache commons-collections4:

CollectionUtils.emptyIfNull(list).stream()
2
  • Hi piotrek; your code might be correct, but it would be better with some context; for example, you could explain how and why this proposed change would resolve the questioner's problem, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems. Commented Aug 17, 2016 at 9:36
  • not sure what should be explained. apache commons-collections is one of the most standard java libraries. how it solves OP problem? it directly does what OP needs - it makes a stream from a nullable list
    – piotrek
    Commented Aug 17, 2016 at 17:09
13

Java 8:

Optional.ofNullable(list)
   .orElseGet(Collections::emptyList)
   .stream()

Java 9:

Stream.ofNullable(collection)
    .flatMap(Collection::stream)

Apache Commons Collections 4:

import org.apache.commons.collections4.CollectionUtils;

CollectionUtils.emptyIfNull(collection)
    .stream()
1

Java 16

Here's an option of generation a Stream from nullable source using Java 16 Stream.mapMulti().

This operation expects an argument of type BiConsumer, i.e. a consumer, which in turn takes two arguments: a stream element and a consumer of the resulting type. Each value offered to the consumer becomes a new stream element, replacing the initial element.

public <T> Stream<T> getStream(Collection<? extends T> c) {

    return Stream.ofNullable(c).mapMulti(Iterable::forEach);
}

Where the method reference Iterable::forEach representing the BiConsumer is an equivalent of the following lambda expression (explicit types provided for descriptiveness):

(Collection<? extends T> coll, Consumer<T> consumer) -> coll.forEach(consumer)

As well as flatMap() operation, mapMulti() is meant to perform one-to-many transformations replacing the consumed original stream element with 0+ (zero or more) elements.

Note that mapMulti() would perform better than flatMap() if you need to flatten a significant number of lists which contain only a few elements or can be empty. Here's a quote from the API Note:

This method is preferable to flatMap in the following circumstances:

  • When replacing each stream element with a small (possibly zero) number of elements. Using this method avoids the overhead of creating a new Stream instance for every group of result elements, as required by flatMap. ...

Note

1. Lists (as well as other Collections and arrays) are containers of data, i.e. when you need something from a list you're interested the actual values are stored inside. A nullable container of data forces you to be right defensive unrelated to your business-logic and you or colleague might forget about that (which creates a problem which might surface at any moment).

Regardless of the source of nullable Collections, if you have a possibility to change the code you're working with, you can eliminate the actual problem, instead of hiding it using Stream.ofNullable() or abusing Optional.

These are possible sources of nullable Collections and treatments for them:

  • A nullable Collection comes from a Custom object wrapped around it. Assign the field with an empty Collection by default.
  • The data was received, for instance, as a JSON payload, and a property representing the Collection is missing or null. That basically the previous case in disguise. Examine the functionality of your parsing tool, most likely you would find a way to assign default values for properties that are absent a come as null.
  • If you have a method returning a nullable Collection, then add a single condition before returning a value instead of having null-checks and null-friendly moves sprinkled all over the code. Refer to the classic book "Effective Java" by Joshua Bloch, Item: "Return empty collections or arrays, not nulls".

2. If you need a quick way to make the code working, but you want to be aware if the incoming list null, then add the conditional statement to reflect the event in the logs:

if (list != null) logger.log( ... );

3. Lastly, as @Stuart Marks has pointed out in his answer to this question Optional wasn't designed for performing/hiding null-checks. And here's a few more references regarding this topic:

0

Personally I consider null deprecated and use Optional wherever possible despite the (tiny) performance overhead. So I use the interface from Stuart Marks with an implementation based on gdejohn, i.e.

@SuppressWarnings("unchecked")
static <T> Stream<T> nullableCollectionToStream(Collection<? extends T> coll)
{
    return (Stream<T>) Optional.ofNullable(coll)
                           .map(Collection::stream)
                           .orElseGet(Stream::empty);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.