0

If I need to group by a property in Java 8, I can do this:

// cars is of type List<Car>
cars.stream().collect(Collectors.groupingBy(Car::getModel));

But how can I group the cars list by a property (e.g. make) of model? I've tried doing something like this:

Map<Make, List<Car>> listOfCarsByMake = cars.stream().collect(Collectors.groupingBy(Car::getModel::getMake));
James
  • 2,876
  • 18
  • 72
  • 116
  • See also http://stackoverflow.com/questions/36798635/call-a-method-on-the-return-value-of-a-method-reference and http://stackoverflow.com/questions/29146199/java-8-chained-method-reference – Tunaki Nov 10 '16 at 22:01
  • Sorry... I missed finding that prior to posting. Thanks for the links. – James Nov 10 '16 at 22:10

0 Answers0