Reputation: 607
Map<Long, Optional<Long>> aMap = new HashMap<>();
This map has some keys and optional values.
Optional<List<Long>> valuesList = input.aMap().values().stream()
.collect(Collectors.toList());
The above way has compilation error. How do i get the optional list correctly?
Upvotes: 4
Views: 813
Reputation: 120848
You don't even need to stream
here, just to collect them to a List
:
List<Optional<Long>> list = new ArrayList<>(input.aMap().values());
Upvotes: 2
Reputation: 48258
you missundertand the return value, dont forget that a Optional<List<Long>>
is an optional object that can have 1 list if present....
you need instead a List<Optional<Long>>
List<Optional<Long>> valuesList = input.aMap()
.values()
.stream()
.collect(Collectors.toList());
Upvotes: 3