I have the following code in my example:
final List<Optional<String>> messages =
ImmutableList.of(Optional.empty(), Optional.empty() );
final String dummy = messages.stream()
.filter( Optional::isPresent )
.map( message -> message.get() )
.collect( Collectors.joining( "\n---\n" ) );
Now on debugging, the value of the variable dummy
is always an empty string. Is that a result of the stream, or the behavior of the Collectors.joining
method?
I was under the assumption that the resultant String would be null.
Optional
?Collectors.joining()
doesn't explicitly document how it handles empty inputs, but I'm not suprised by an empty string. Indeed I wouldn't expectnull
to be returned by this ever.