Java 8 stream groupingBy
Tell me how to convert:
Map<String, List<Pet>> pets = petList
.stream()
.collect(Collectors.groupingBy(Pet::getType));
In
Map<String, PetsHouse>
Using stream
?
Or do I bother too much and decide without using streams?
PetHouse
- it's just a kind of wrapper over the sheet
List<Pet> petList;
PetHouse house = new PetHouse(petList);
2
2 answers
Everything is solved:
Map<String, List<Pet>> pets = petList
.stream()
.collect(Collectors.groupingBy(
Pet::getType,
Collectors.collectingAndThen(Collectors.toList(), PetHouse::new)
));
4
Author: Alex Chermenin, 2018-01-10 13:39:52
Map<String, PetHouse> m = pets.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, e -> new PetHouse(e.getValue())));
5
Author: Nikolai, 2018-01-10 13:23:51