Your second attempt is not viable because method forEach()
is void
. To do the job in a single statement, you need a Stream.
But there is more to it.
Storing Optional
objects into a Collection
is an antipattern. It almost the same as storing null
references because optional not necessarily contains a value.
Both null
and empty optional are meant to represent the absence of data and nothing else. If one of them has a separate meaning in your application (apart from "no value"), it's a smell.
If you need to fire a particular action when a call getUserByUserId()
returns an empty optional, then do it right after receiving the optional instead of placing it to the list (and by the way, Stream is not the right tool when you need to perform some side-effects along the way, it would be better to stick with plain loops).
Here's a small quote from the answer by @Stuart Marks, Java and OpenJDK developer:
I'm sure somebody could come up with some contrived cases where they really want to store an Optional
in a field or a collection, but in general, it is best to avoid doing this.
I suggest you to check the presence of value in the Optional
right on the spot (in the stream) and then "unpack" non-empty optional objects.
That's how it might look like:
List<User> pmUsers = pmUserIds.stream()
.map(userRepository::getUserByUserId) // Stream<Optional<User>>
.filter(Optional::isPresent)
.map(Optional::get) // Stream<User>
.toList(); // for Java 16+ or .collect(Collectors.toList()) for earlier versions