I need to convert a list of objects to a map of sets of one of the objects' property and I'd like to use Java streams for that.
For example there is a List<Dog> and I'd like to convert it to a HashMap where key is dog age and value is Set of breeds.
@Getter //lombok for getters
public class Dog {
private String name;
private int age;
private int breed;
}
I can create Map<Integer, Set<Dog>> dogsByAge a by using this statement.
Map<Integer, Set<Dog>> dogsByAge = dogs.stream()
.collect(groupingBy(Dog::getAge, toSet()));
However I need Map<Integer, Set<String>> breedsByAge
Is that possible using streams?