Reputation: 687
I have a HashMap that consists of a list of objects:
Map<String, List<Employee>> map;
I would like to extract all the employee objects from each list into a new list.
I tried to do the following but either does not give me what I want or is an error.
List<Asset> as = selectedAssetsMap.values().stream().findAny().get().stream().collect(Collectors.toList());
List<Asset> s = selectedAssetsMap.values().stream().forEach(l -> 1.stream().collect(Collectors.toList()));
List<Asset> a1 = selectedAssetsMap.values().stream().map(l -> l.collect(Collectors.toList()));
The first version gives me only the first set of Employee objects. The second one are wrong... Any help/direction will be very helpful
Upvotes: 1
Views: 360
Reputation: 21124
You may use the flatMap
operator like this.
List<Employee> allEmps = map.values().stream()
.flatMap(List::stream)
.collect(Collectors.toList());
Upvotes: 2