I'm trying to learn more about the streams in java and I was wondering how can I use the toMap to change a List of items into a map that has a property of those items as key and a sublist of the same List as value. For example if I have an class:
public class Car {
private String color;
private String model;
// getters and setters
}
if I have: List<Car> carList;, how can I get a Map<String, List<Car>> where the entries will be something like:
"BLUE" : {car1, car3, car7},
"RED" : {car2, car5, car6, car8},
"WHITE" : {car4, car9}
I understand that in order to get it I can run something like the following statement however I don't understand what the second parameter should be:
Map<String, List<Car>> carsMap = list.stream()
.collect(Collectors.toMap(Car::getColor, ?));
would someone please help me get this correct?