|
1 | | -package lambdasinaction.chap6; |
2 | | - |
3 | | -import java.util.*; |
4 | | - |
5 | | -import static java.util.stream.Collectors.*; |
6 | | -import static lambdasinaction.chap6.Dish.dishTags; |
7 | | -import static lambdasinaction.chap6.Dish.menu; |
8 | | - |
9 | | -public class Grouping { |
10 | | - |
11 | | - enum CaloricLevel { DIET, NORMAL, FAT }; |
12 | | - |
13 | | - public static void main(String ... args) { |
14 | | - System.out.println("Dishes grouped by type: " + groupDishesByType()); |
15 | | - System.out.println("Dish names grouped by type: " + groupDishNamesByType()); |
16 | | - System.out.println("Dish tags grouped by type: " + groupDishTagsByType()); |
17 | | - System.out.println("Caloric dishes grouped by type: " + groupCaloricDishesByType()); |
18 | | - System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel()); |
19 | | - System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel()); |
20 | | - System.out.println("Count dishes in groups: " + countDishesInGroups()); |
21 | | - System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType()); |
22 | | - System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals()); |
23 | | - System.out.println("Sum calories by type: " + sumCaloriesByType()); |
24 | | - System.out.println("Caloric levels by type: " + caloricLevelsByType()); |
25 | | - } |
26 | | - |
27 | | - private static Map<Dish.Type, List<Dish>> groupDishesByType() { |
28 | | - return menu.stream().collect(groupingBy(Dish::getType)); |
29 | | - } |
30 | | - |
31 | | - private static Map<Dish.Type, List<String>> groupDishNamesByType() { |
32 | | - return menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList()))); |
33 | | - } |
34 | | - |
35 | | - private static Map<Dish.Type, Set<String>> groupDishTagsByType() { |
36 | | - return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet()))); |
37 | | - } |
38 | | - |
39 | | - private static Map<Dish.Type, List<Dish>> groupCaloricDishesByType() { |
40 | | -// return menu.stream().filter(dish -> dish.getCalories() > 500).collect(groupingBy(Dish::getType)); |
41 | | - return menu.stream().collect(groupingBy(Dish::getType, filtering(dish -> dish.getCalories() > 500, toList()))); |
42 | | - } |
43 | | - |
44 | | - private static Map<CaloricLevel, List<Dish>> groupDishesByCaloricLevel() { |
45 | | - return menu.stream().collect( |
46 | | - groupingBy(dish -> { |
47 | | - if (dish.getCalories() <= 400) return CaloricLevel.DIET; |
48 | | - else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; |
49 | | - else return CaloricLevel.FAT; |
50 | | - } )); |
51 | | - } |
52 | | - |
53 | | - private static Map<Dish.Type, Map<CaloricLevel, List<Dish>>> groupDishedByTypeAndCaloricLevel() { |
54 | | - return menu.stream().collect( |
55 | | - groupingBy(Dish::getType, |
56 | | - groupingBy((Dish dish) -> { |
57 | | - if (dish.getCalories() <= 400) return CaloricLevel.DIET; |
58 | | - else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; |
59 | | - else return CaloricLevel.FAT; |
60 | | - } ) |
61 | | - ) |
62 | | - ); |
63 | | - } |
64 | | - |
65 | | - private static Map<Dish.Type, Long> countDishesInGroups() { |
66 | | - return menu.stream().collect(groupingBy(Dish::getType, counting())); |
67 | | - } |
68 | | - |
69 | | - private static Map<Dish.Type, Optional<Dish>> mostCaloricDishesByType() { |
70 | | - return menu.stream().collect( |
71 | | - groupingBy(Dish::getType, |
72 | | - reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2))); |
73 | | - } |
74 | | - |
75 | | - private static Map<Dish.Type, Dish> mostCaloricDishesByTypeWithoutOprionals() { |
76 | | - return menu.stream().collect( |
77 | | - groupingBy(Dish::getType, |
78 | | - collectingAndThen( |
79 | | - reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2), |
80 | | - Optional::get))); |
81 | | - } |
82 | | - |
83 | | - private static Map<Dish.Type, Integer> sumCaloriesByType() { |
84 | | - return menu.stream().collect(groupingBy(Dish::getType, |
85 | | - summingInt(Dish::getCalories))); |
86 | | - } |
87 | | - |
88 | | - private static Map<Dish.Type, Set<CaloricLevel>> caloricLevelsByType() { |
89 | | - return menu.stream().collect( |
90 | | - groupingBy(Dish::getType, mapping( |
91 | | - dish -> { if (dish.getCalories() <= 400) return CaloricLevel.DIET; |
92 | | - else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; |
93 | | - else return CaloricLevel.FAT; }, |
94 | | - toSet() ))); |
95 | | - } |
96 | | -} |
| 1 | +//package lambdasinaction.chap6; |
| 2 | +// |
| 3 | +//import java.util.*; |
| 4 | +// |
| 5 | +//import static java.util.stream.Collectors.*; |
| 6 | +//import static lambdasinaction.chap6.Dish.dishTags; |
| 7 | +//import static lambdasinaction.chap6.Dish.menu; |
| 8 | +// |
| 9 | +//public class Grouping { |
| 10 | +// |
| 11 | +// enum CaloricLevel { DIET, NORMAL, FAT }; |
| 12 | +// |
| 13 | +// public static void main(String ... args) { |
| 14 | +// System.out.println("Dishes grouped by type: " + groupDishesByType()); |
| 15 | +// System.out.println("Dish names grouped by type: " + groupDishNamesByType()); |
| 16 | +// System.out.println("Dish tags grouped by type: " + groupDishTagsByType()); |
| 17 | +// System.out.println("Caloric dishes grouped by type: " + groupCaloricDishesByType()); |
| 18 | +// System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel()); |
| 19 | +// System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel()); |
| 20 | +// System.out.println("Count dishes in groups: " + countDishesInGroups()); |
| 21 | +// System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType()); |
| 22 | +// System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals()); |
| 23 | +// System.out.println("Sum calories by type: " + sumCaloriesByType()); |
| 24 | +// System.out.println("Caloric levels by type: " + caloricLevelsByType()); |
| 25 | +// } |
| 26 | +// |
| 27 | +// private static Map<Dish.Type, List<Dish>> groupDishesByType() { |
| 28 | +// return menu.stream().collect(groupingBy(Dish::getType)); |
| 29 | +// } |
| 30 | +// |
| 31 | +// private static Map<Dish.Type, List<String>> groupDishNamesByType() { |
| 32 | +// return menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList()))); |
| 33 | +// } |
| 34 | +// |
| 35 | +// private static Map<Dish.Type, Set<String>> groupDishTagsByType() { |
| 36 | +// return menu.stream().collect(groupingBy(Dish::getType, flatMapping(dish -> dishTags.get( dish.getName() ).stream(), toSet()))); |
| 37 | +// } |
| 38 | +// |
| 39 | +// private static Map<Dish.Type, List<Dish>> groupCaloricDishesByType() { |
| 40 | +//// return menu.stream().filter(dish -> dish.getCalories() > 500).collect(groupingBy(Dish::getType)); |
| 41 | +// return menu.stream().collect(groupingBy(Dish::getType, filtering(dish -> dish.getCalories() > 500, toList()))); |
| 42 | +// } |
| 43 | +// |
| 44 | +// private static Map<CaloricLevel, List<Dish>> groupDishesByCaloricLevel() { |
| 45 | +// return menu.stream().collect( |
| 46 | +// groupingBy(dish -> { |
| 47 | +// if (dish.getCalories() <= 400) return CaloricLevel.DIET; |
| 48 | +// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; |
| 49 | +// else return CaloricLevel.FAT; |
| 50 | +// } )); |
| 51 | +// } |
| 52 | +// |
| 53 | +// private static Map<Dish.Type, Map<CaloricLevel, List<Dish>>> groupDishedByTypeAndCaloricLevel() { |
| 54 | +// return menu.stream().collect( |
| 55 | +// groupingBy(Dish::getType, |
| 56 | +// groupingBy((Dish dish) -> { |
| 57 | +// if (dish.getCalories() <= 400) return CaloricLevel.DIET; |
| 58 | +// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; |
| 59 | +// else return CaloricLevel.FAT; |
| 60 | +// } ) |
| 61 | +// ) |
| 62 | +// ); |
| 63 | +// } |
| 64 | +// |
| 65 | +// private static Map<Dish.Type, Long> countDishesInGroups() { |
| 66 | +// return menu.stream().collect(groupingBy(Dish::getType, counting())); |
| 67 | +// } |
| 68 | +// |
| 69 | +// private static Map<Dish.Type, Optional<Dish>> mostCaloricDishesByType() { |
| 70 | +// return menu.stream().collect( |
| 71 | +// groupingBy(Dish::getType, |
| 72 | +// reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2))); |
| 73 | +// } |
| 74 | +// |
| 75 | +// private static Map<Dish.Type, Dish> mostCaloricDishesByTypeWithoutOprionals() { |
| 76 | +// return menu.stream().collect( |
| 77 | +// groupingBy(Dish::getType, |
| 78 | +// collectingAndThen( |
| 79 | +// reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2), |
| 80 | +// Optional::get))); |
| 81 | +// } |
| 82 | +// |
| 83 | +// private static Map<Dish.Type, Integer> sumCaloriesByType() { |
| 84 | +// return menu.stream().collect(groupingBy(Dish::getType, |
| 85 | +// summingInt(Dish::getCalories))); |
| 86 | +// } |
| 87 | +// |
| 88 | +// private static Map<Dish.Type, Set<CaloricLevel>> caloricLevelsByType() { |
| 89 | +// return menu.stream().collect( |
| 90 | +// groupingBy(Dish::getType, mapping( |
| 91 | +// dish -> { if (dish.getCalories() <= 400) return CaloricLevel.DIET; |
| 92 | +// else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL; |
| 93 | +// else return CaloricLevel.FAT; }, |
| 94 | +// toSet() ))); |
| 95 | +// } |
| 96 | +//} |
0 commit comments