Using streams for a null-safe conversion from an array to list

You might use the map :

List<String> ccAddrs = Optional.ofNullable(ccAddress)
                               .map(Arrays::asList)
                               .orElse(Collections.emptyList())

In my opinion, the code that you have so far is perfectly readable. I think using a Stream for something like this will just complicate things. If you still want to use a Stream, then something like the following would work:

mailObject.setCcAddresses(Stream.ofNullable(ccAddresses)
          .flatMap(Arrays::stream)
          .collect(Collectors.toUnmodifiableList()));

As you can see, this is a more unreadable, and I wouldn't recommend it over your simple ternary expression.


Your Optional solution is slightly more readable and would look like the following:

mailObject.setCcAddresses(Optional.ofNullable(ccAddresses)
          .map(Arrays::asList)
          .orElse(Collections.emptyList()));