Skip to content

Commit e764de4

Browse files
committed
update for partitioning function
1 parent a8f9508 commit e764de4

2 files changed

Lines changed: 101 additions & 95 deletions

File tree

Lines changed: 90 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,91 @@
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));
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("Caloric dishes grouped by type: " + groupCaloricDishesByType());
17+
System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel());
18+
System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel());
19+
System.out.println("Count dishes in groups: " + countDishesInGroups());
20+
System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType());
21+
System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals());
22+
System.out.println("Sum calories by type: " + sumCaloriesByType());
23+
System.out.println("Caloric levels by type: " + caloricLevelsByType());
24+
}
25+
26+
private static Map<Dish.Type, List<Dish>> groupDishesByType() {
27+
return menu.stream().collect(groupingBy(Dish::getType));
28+
}
29+
30+
private static Map<Dish.Type, List<String>> groupDishNamesByType() {
31+
return menu.stream().collect(groupingBy(Dish::getType, mapping(Dish::getName, toList())));
32+
}
33+
34+
private static Map<Dish.Type, List<Dish>> groupCaloricDishesByType() {
35+
return menu.stream().filter(dish -> dish.getCalories() > 500).collect(groupingBy(Dish::getType));
4136
// 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-
//}
37+
}
38+
39+
private static Map<CaloricLevel, List<Dish>> groupDishesByCaloricLevel() {
40+
return menu.stream().collect(
41+
groupingBy(dish -> {
42+
if (dish.getCalories() <= 400) return CaloricLevel.DIET;
43+
else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
44+
else return CaloricLevel.FAT;
45+
} ));
46+
}
47+
48+
private static Map<Dish.Type, Map<CaloricLevel, List<Dish>>> groupDishedByTypeAndCaloricLevel() {
49+
return menu.stream().collect(
50+
groupingBy(Dish::getType,
51+
groupingBy((Dish dish) -> {
52+
if (dish.getCalories() <= 400) return CaloricLevel.DIET;
53+
else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
54+
else return CaloricLevel.FAT;
55+
} )
56+
)
57+
);
58+
}
59+
60+
private static Map<Dish.Type, Long> countDishesInGroups() {
61+
return menu.stream().collect(groupingBy(Dish::getType, counting()));
62+
}
63+
64+
private static Map<Dish.Type, Optional<Dish>> mostCaloricDishesByType() {
65+
return menu.stream().collect(
66+
groupingBy(Dish::getType,
67+
reducing((Dish d1, Dish d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)));
68+
}
69+
70+
private static Map<Dish.Type, Dish> mostCaloricDishesByTypeWithoutOprionals() {
71+
return menu.stream().collect(
72+
groupingBy(Dish::getType,
73+
collectingAndThen(
74+
reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2),
75+
Optional::get)));
76+
}
77+
78+
private static Map<Dish.Type, Integer> sumCaloriesByType() {
79+
return menu.stream().collect(groupingBy(Dish::getType,
80+
summingInt(Dish::getCalories)));
81+
}
82+
83+
private static Map<Dish.Type, Set<CaloricLevel>> caloricLevelsByType() {
84+
return menu.stream().collect(
85+
groupingBy(Dish::getType, mapping(
86+
dish -> { if (dish.getCalories() <= 400) return CaloricLevel.DIET;
87+
else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
88+
else return CaloricLevel.FAT; },
89+
toSet() )));
90+
}
91+
}

src/main/java/lambdasinaction/chap6/Partitioning.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public static void main(String ... args) {
1212
System.out.println("Dishes partitioned by vegetarian: " + partitionByVegeterian());
1313
System.out.println("Vegetarian Dishes by type: " + vegetarianDishesByType());
1414
System.out.println("Most caloric dishes by vegetarian: " + mostCaloricPartitionedByVegetarian());
15+
System.out.println("Vegetarian Dishes by calories: " + vegetarianDishesByCalories());
16+
System.out.println("Vegetarian Dishes by counting: " + vegetarianDishesCounting());
1517
}
1618

1719
private static Map<Boolean, List<Dish>> partitionByVegeterian() {
@@ -22,6 +24,15 @@ private static Map<Boolean, Map<Dish.Type, List<Dish>>> vegetarianDishesByType()
2224
return menu.stream().collect(partitioningBy(Dish::isVegetarian, groupingBy(Dish::getType)));
2325
}
2426

27+
private static Map<Boolean, Map<Boolean, List<Dish>>> vegetarianDishesByCalories() {
28+
return menu.stream().collect(partitioningBy(Dish:: isVegetarian, partitioningBy(d -> d.getCalories() > 500)));
29+
}
30+
31+
private static Map<Boolean, Long> vegetarianDishesCounting() {
32+
return menu.stream().collect(partitioningBy(Dish:: isVegetarian, counting()));
33+
}
34+
35+
2536
private static Object mostCaloricPartitionedByVegetarian() {
2637
return menu.stream().collect(
2738
partitioningBy(Dish::isVegetarian,

0 commit comments

Comments
 (0)