![](https://csdnimg.cn/release/download_crawler_static/85860819/bg1.jpg)
Java 8
Collectors.groupingBy
Map<Dish.Type, List<Dish>> dishesByType =
menu.stream().collect(groupingBy(Dish::getType));
Map
{FISH=[prawns, salmon], OTHER=[french fries, rice, season fruit, pizza],
MEAT=[pork, beef, chicken]}
groupingBy
Function
Dish
Dish.Type
Function
6-4
Map
1
2
3
4
8
10
7
11
6-4
400
“
diet
400
700
“
normal
700
“
fat
Dish
Lambda
public enum CaloricLevel { DIET, NORMAL, FAT }
Map<CaloricLevel, List<Dish>> dishesByCaloricLevel = menu.stream().collect(
groupingBy(dish -> {
if (dish.getCalories() <= 400) return CaloricLevel.DIET;
else if (dish.getCalories() <= 700) return
CaloricLevel.NORMAL;
else return CaloricLevel.FAT;
} ));