Java stream - Sort a List to a HashMap of Lists
The great answer above can further be improved by method reference notation:
List<Dog> dogs = ...
Map<String, List<Dog>> map = dogs.stream()
.collect(Collectors.groupingBy(Dog::getBreed));
You can do it with groupingBy
.
Assuming that your input is a List<Dog>
, the Map
member inside the Dog
class is called map
, and the Breed is stored for the "Breed" key :
List<Dog> dogs = ...
Map<String, List<Dog>> map = dogs.stream()
.collect(Collectors.groupingBy(d -> d.map.get("Breed")));