forked from benjaminwhx/p_java8InAction
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrouping.java
More file actions
83 lines (69 loc) · 3.37 KB
/
Grouping.java
File metadata and controls
83 lines (69 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package chapter6;
import entity.Dish;
import java.util.*;
import static java.util.stream.Collectors.*;
/**
* User: 吴海旭
* Date: 2016-11-22
* Time: 下午0:27
* 6.3:分组
*/
public class Grouping {
enum CaloricLevel { DIET, NORMAL, FAT }
public static void main(String[] args) {
System.out.println("Dishes grouped by type: " + groupDishesByType());
System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel());
System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel());
System.out.println("Count dishes in groups: " + countDishesInGroups());
System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType());
System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOptionals());
System.out.println("Sum calories by type: " + sumCaloriesByType());
System.out.println("Caloric levels by type: " + caloricLevelsByType());
}
private static Map<Dish.Type, List<Dish>> groupDishesByType() {
return Dish.menu.stream().collect(groupingBy(Dish::getType));
}
private static Map<CaloricLevel, List<Dish>> groupDishesByCaloricLevel() {
return Dish.menu.stream().collect(groupingBy(t -> {
if (t.getCalories() <= 400) return CaloricLevel.DIET;
else if (t.getCalories() <= 700) return CaloricLevel.NORMAL;
else return CaloricLevel.FAT;
}));
}
private static Map<Dish.Type, Map<CaloricLevel, List<Dish>>> groupDishedByTypeAndCaloricLevel() {
return Dish.menu.stream().collect(groupingBy(Dish::getType, groupingBy(t -> {
if (t.getCalories() <= 400) return CaloricLevel.DIET;
else if (t.getCalories() <= 700) return CaloricLevel.NORMAL;
else return CaloricLevel.FAT;
})));
}
private static Map<Dish.Type, Long> countDishesInGroups() {
return Dish.menu.stream().collect(groupingBy(Dish::getType, counting()));
}
private static Map<Dish.Type, Optional<Dish>> mostCaloricDishesByType() {
return Dish.menu.stream().collect(groupingBy(Dish::getType, maxBy(Comparator.comparing(Dish::getCalories))));
// return Dish.menu.stream().collect(
// groupingBy(Dish::getType,
// reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)));
}
private static Map<Dish.Type, Dish> mostCaloricDishesByTypeWithoutOptionals() {
return Dish.menu.stream().collect(
groupingBy(Dish::getType,
collectingAndThen(
maxBy(Comparator.comparing(Dish::getCalories)), Optional::get)
));
}
private static Map<Dish.Type, Integer> sumCaloriesByType() {
return Dish.menu.stream().collect(groupingBy(Dish::getType, summingInt(Dish::getCalories)));
}
private static Map<Dish.Type, Set<CaloricLevel>> caloricLevelsByType() {
return Dish.menu.stream().collect(
groupingBy(Dish::getType,
mapping(t -> {
if (t.getCalories() <= 400) return CaloricLevel.DIET;
else if (t.getCalories() <= 700) return CaloricLevel.NORMAL;
else return CaloricLevel.FAT;
}, toSet())
));
}
}