While working with Streams in Java 11, I noticed the following situation. I'm trying to use the Stream API in two different ways on a non-null list.
First way:
Collection<Object> categoryIds = List.of(1L, 2L, 3L, 4L);
List<String> nullable = Stream.ofNullable(categoryIds)
.map(Object::toString)
.collect(Collectors.toList());
Second way:
Collection<Object> categoryIds = List.of(1L, 2L, 3L, 4L);
List<String> nullable = categoryIds.stream()
.map(Object::toString)
.collect(Collectors.toList());
The two ways I tried lead to two different results. The resulting list of the first way is a string containing a single element. This string is: "[1, 2, 3, 4]" The second way gives the desired and expected results. Returns a result list with 4 elements: "1", "2", "3", "4".
Is this behavior expected? Or is it a problem?
Stream.ofNullable you are creating a
Streamobject with single element or empty Stream for null objectIf you want to get the same output using
Stream.ofNullableyou need to flatten the list usingflatMapCollection.stream - it creates the stream object with collection elements in sequential order
And the
Collection.streamis equivalent to stream.of(T... values) method