Suppose I have a group of bumper cars, which have a size, a color and an identifier (\"car code\") on their sides.
class BumperCar {
int size;
String col
You can collect by by using BiConsumer
that take (HashMap
as parameters
Collection values = bumperCars.stream()
.collect(HashMap::new, (HashMap res, BumperCar bc) -> {
SizeColorCombination dg = new SizeColorCombination(bc.color, bc.size);
DistGroup distGroup = res.get(dg);
if(distGroup != null) {
distGroup.addCarCode(bc.carCode);
}else {
List codes = new ArrayList();
distGroup = new DistGroup(bc.size, bc.color, codes);
res.put(dg, distGroup);
}
},HashMap::putAll).values();