|
| 1 | +package com.brianway.learning.java8.streamapi.collect; |
| 2 | + |
| 3 | +import com.brianway.learning.java8.streamapi.Dish; |
| 4 | +import static com.brianway.learning.java8.streamapi.Dish.menu; |
| 5 | +import static java.util.stream.Collectors.collectingAndThen; |
| 6 | +import static java.util.stream.Collectors.counting; |
| 7 | +import static java.util.stream.Collectors.groupingBy; |
| 8 | +import static java.util.stream.Collectors.mapping; |
| 9 | +import static java.util.stream.Collectors.maxBy; |
| 10 | +import static java.util.stream.Collectors.summingInt; |
| 11 | +import static java.util.stream.Collectors.toSet; |
| 12 | + |
| 13 | +import java.util.Comparator; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Optional; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +/** |
| 20 | + * 分组 |
| 21 | + */ |
| 22 | +public class Grouping { |
| 23 | + |
| 24 | + private enum CaloricLevel { |
| 25 | + DIET, |
| 26 | + NORMAL, |
| 27 | + FAT |
| 28 | + } |
| 29 | + |
| 30 | + public static void main(String... args) { |
| 31 | + System.out.println("Dishes grouped by type: " + groupDishesByType()); |
| 32 | + System.out.println("Dishes grouped by caloric level: " + groupDishesByCaloricLevel()); |
| 33 | + System.out.println("Dishes grouped by type and caloric level: " + groupDishedByTypeAndCaloricLevel()); |
| 34 | + System.out.println("Count dishes in groups: " + countDishesInGroups()); |
| 35 | + System.out.println("Most caloric dishes by type: " + mostCaloricDishesByType()); |
| 36 | + System.out.println("Most caloric dishes by type: " + mostCaloricDishesByTypeWithoutOprionals()); |
| 37 | + System.out.println("Sum calories by type: " + sumCaloriesByType()); |
| 38 | + System.out.println("Caloric levels by type: " + caloricLevelsByType()); |
| 39 | + } |
| 40 | + |
| 41 | + private static CaloricLevel getCaloricLevel(Dish dish) { |
| 42 | + if (dish.getCalories() <= 400) { |
| 43 | + return CaloricLevel.DIET; |
| 44 | + } else if (dish.getCalories() <= 700) { |
| 45 | + return CaloricLevel.NORMAL; |
| 46 | + } else { |
| 47 | + return CaloricLevel.FAT; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private static Map<Dish.Type, List<Dish>> groupDishesByType() { |
| 52 | + return menu.stream().collect(groupingBy(Dish::getType)); |
| 53 | + } |
| 54 | + |
| 55 | + private static Map<CaloricLevel, List<Dish>> groupDishesByCaloricLevel() { |
| 56 | + return menu.stream().collect( |
| 57 | + groupingBy(Grouping::getCaloricLevel)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * 多级分组 |
| 62 | + */ |
| 63 | + private static Map<Dish.Type, Map<CaloricLevel, List<Dish>>> groupDishedByTypeAndCaloricLevel() { |
| 64 | + return menu.stream().collect( |
| 65 | + groupingBy(Dish::getType, |
| 66 | + groupingBy(Grouping::getCaloricLevel) |
| 67 | + ) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * 按子数组收集数据 |
| 73 | + */ |
| 74 | + private static Map<Dish.Type, Long> countDishesInGroups() { |
| 75 | + return menu.stream().collect(groupingBy(Dish::getType, counting())); |
| 76 | + } |
| 77 | + |
| 78 | + private static Map<Dish.Type, Optional<Dish>> mostCaloricDishesByType() { |
| 79 | + return menu.stream().collect( |
| 80 | + groupingBy(Dish::getType, |
| 81 | + maxBy(Comparator.comparingInt(Dish::getCalories))) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * 按子数组收集数据,把收集器的结果转换为另一种类型 |
| 87 | + */ |
| 88 | + private static Map<Dish.Type, Dish> mostCaloricDishesByTypeWithoutOprionals() { |
| 89 | + return menu.stream().collect( |
| 90 | + groupingBy(Dish::getType, |
| 91 | + collectingAndThen( |
| 92 | + maxBy(Comparator.comparingInt(Dish::getCalories)), |
| 93 | + Optional::get))); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * 与 groupingBy 联合使用的其他收集器例子 |
| 98 | + */ |
| 99 | + private static Map<Dish.Type, Integer> sumCaloriesByType() { |
| 100 | + return menu.stream().collect(groupingBy(Dish::getType, |
| 101 | + summingInt(Dish::getCalories))); |
| 102 | + } |
| 103 | + |
| 104 | + private static Map<Dish.Type, Set<CaloricLevel>> caloricLevelsByType() { |
| 105 | + return menu.stream().collect( |
| 106 | + groupingBy(Dish::getType, mapping( |
| 107 | + Grouping::getCaloricLevel, |
| 108 | + toSet()))); |
| 109 | + } |
| 110 | +} |
0 commit comments