zoukankan      html  css  js  c++  java
  • 按子组收集数据

    package com.ant.jdk8.chap06;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.stream.Collectors;
    
    public class DishGroupDemo {
        public static void main(String[] args) {
            List<Dish> menu = Arrays.asList(
                    new Dish("pork", false, 800, Type.MEAT),
                    new Dish("beef", false, 700, Type.MEAT),
                    new Dish("chicken", false, 400, Type.MEAT),
                    new Dish("french fries", true, 530, Type.OTHER),
                    new Dish("rice", true, 350, Type.OTHER),
                    new Dish("season fruit", true, 120, Type.OTHER),
                    new Dish("pizza", true, 550, Type.OTHER),
                    new Dish("prawns", false, 300, Type.FISH),
                    new Dish("salmon", false, 450, Type.FISH) );
            Map<Type,Long> typesCount =
                    menu.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.counting()));
            System.out.println(typesCount);
        }
    }
    

    package com.ant.jdk8.chap06;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class DishGroupDemo {
        public static void main(String[] args) {
            List<Dish> menu = Arrays.asList(
                    new Dish("pork", false, 800, Type.MEAT),
                    new Dish("beef", false, 700, Type.MEAT),
                    new Dish("chicken", false, 400, Type.MEAT),
                    new Dish("french fries", true, 530, Type.OTHER),
                    new Dish("rice", true, 350, Type.OTHER),
                    new Dish("season fruit", true, 120, Type.OTHER),
                    new Dish("pizza", true, 550, Type.OTHER),
                    new Dish("prawns", false, 300, Type.FISH),
                    new Dish("salmon", false, 450, Type.FISH) );
            Map<Type, Optional<Dish>> mostCaloriesByType =
                    menu.stream().collect(Collectors.groupingBy(Dish::getType,
                            Collectors.maxBy(Comparator.comparingInt(Dish::getCalories))));
            System.out.println(mostCaloriesByType);
        }
    }
    

    1. 把收集器的结果转换为另一种类型:

    package com.ant.jdk8.chap06;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class DishGroupDemo {
        public static void main(String[] args) {
            List<Dish> menu = Arrays.asList(
                    new Dish("pork", false, 800, Type.MEAT),
                    new Dish("beef", false, 700, Type.MEAT),
                    new Dish("chicken", false, 400, Type.MEAT),
                    new Dish("french fries", true, 530, Type.OTHER),
                    new Dish("rice", true, 350, Type.OTHER),
                    new Dish("season fruit", true, 120, Type.OTHER),
                    new Dish("pizza", true, 550, Type.OTHER),
                    new Dish("prawns", false, 300, Type.FISH),
                    new Dish("salmon", false, 450, Type.FISH) );
            Map<Type, Dish> mostCaloriesByType =
                    menu.stream().collect(Collectors.groupingBy(Dish::getType,
                            Collectors.collectingAndThen(
                                    Collectors.maxBy(Comparator.comparingInt(Dish::getCalories)),Optional::get)));
            System.out.println(mostCaloriesByType);
        }
    }

    2. 与groupingBy联合使用的其他收集器的例子:

    package com.ant.jdk8.chap06;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class DishGroupDemo {
        public static void main(String[] args) {
            List<Dish> menu = Arrays.asList(
                    new Dish("pork", false, 800, Type.MEAT),
                    new Dish("beef", false, 700, Type.MEAT),
                    new Dish("chicken", false, 400, Type.MEAT),
                    new Dish("french fries", true, 530, Type.OTHER),
                    new Dish("rice", true, 350, Type.OTHER),
                    new Dish("season fruit", true, 120, Type.OTHER),
                    new Dish("pizza", true, 550, Type.OTHER),
                    new Dish("prawns", false, 300, Type.FISH),
                    new Dish("salmon", false, 450, Type.FISH) );
            Map<Type, Integer> totalCaloriesByType =
                    menu.stream().collect(Collectors.groupingBy(Dish::getType,
                            Collectors.summingInt(Dish::getCalories)));
            System.out.println(totalCaloriesByType);
        }
    }

    package com.ant.jdk8.chap06;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class DishGroupDemo {
        public static void main(String[] args) {
            List<Dish> menu = Arrays.asList(
                    new Dish("pork", false, 800, Type.MEAT),
                    new Dish("beef", false, 700, Type.MEAT),
                    new Dish("chicken", false, 400, Type.MEAT),
                    new Dish("french fries", true, 530, Type.OTHER),
                    new Dish("rice", true, 350, Type.OTHER),
                    new Dish("season fruit", true, 120, Type.OTHER),
                    new Dish("pizza", true, 550, Type.OTHER),
                    new Dish("prawns", false, 300, Type.FISH),
                    new Dish("salmon", false, 450, Type.FISH) );
            Map<Type, Set<CaloricLevel>> caloricLevelByType =
                    menu.stream().collect(Collectors.groupingBy(Dish::getType,
                            Collectors.mapping(d->{if(d.getCalories()<=400) return CaloricLevel.DIET;
                            else if(d.getCalories()<=700) return CaloricLevel.NORMAL;
                            else return CaloricLevel.FAT;},
                                    Collectors.toSet())));
            System.out.println(caloricLevelByType);
        }
    }
    

  • 相关阅读:
    基于51单片机PWM调速L298芯片控制两选一直流电机正反运转的项目工程
    基于51单片机四位一体数码管显示短按加或减数值及长按连加或连减数值的项目工程
    Android 多语言动态更新方案探索
    图解 Promise 实现原理(一)—— 基础实现
    vivo 大规模特征存储实践
    揭秘 vivo 如何打造千万级 DAU 活动中台
    前端科普系列(2):Node.js 换个角度看世界
    分布式定时任务调度框架实践
    深入学习和理解 Redux
    前端科普系列(1):前端简史
  • 原文地址:https://www.cnblogs.com/i-hard-working/p/9595879.html
Copyright © 2011-2022 走看看