stream流的一些常用用法

发布时间 2023-06-30 16:31:25作者: 大山008
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Employee {
    private Long id;
    private String name;   //
    private String city;   // 城市
    private Integer sales;  // 销售额
    private boolean status;  // 销售额
}

常用方法  @Slf4j

public class StreamTest {

    static List<Employee> list = Arrays.asList(
            new Employee(1l,"李念", "西安", 800,true),
            new Employee(2l,"李四", "西安", 700,false),
            new Employee(3l,"王五", "西安", 400,false),
            new Employee(4l,"李东", "武汉", 530,true),
            new Employee(5l,"赵乐", "武汉", 350,false),
            new Employee(6l,"王卓", "武汉", 120,false),
            new Employee(7l,"皇子", "成都", 550,true),
            new Employee(1l,"李念", "成都", 300,true),
            new Employee(9l,"梁博", "成都", 450,false));

    //编辑集合并统一给状态赋值
    public static void testForEach() {
        list.stream().forEach(s ->
                s.setStatus(true)
        );
        log.info("ForEach输出:数据:{},大小:{}", list.toArray(), list.size());
    }

    //过滤出销售额大于400的员工
    public static void testFilter() {
        List<Employee> listfilter = list.stream().filter(s->s.getSales() > 400).sorted(Comparator.comparing(Employee::getSales)).collect(Collectors.toList());
        log.info("Filter输出:数据:{},大小:{}", listfilter, listfilter.size());
    }

    //将员工名称转成集合并去掉前两个
    public static List<String> testMap() {
        List<String> listMap = list.stream().map(Employee::getName).skip(2).collect(Collectors.toList());
        log.info("Map输出:数据:{},大小:{}", listMap, listMap.size());
        return listMap;
    }

    //过滤出状态是true 的前7组数据
    public static void testFilterB() {
        List<Employee> listfilterB = list.stream().filter(Employee::isStatus).limit(7).collect(Collectors.toList());
        log.info("FilterB输出:数据:{},大小:{}", listfilterB, listfilterB.size());
    }

    //按城市分组,key为城市名称
    public static void testGroupBy1(){
       Map<String,List<Employee>> map = list.stream().collect(Collectors.groupingBy(Employee::getCity));
        log.info("testGroupBy输出:数据:{},大小:{}", map, map.size());
        log.info("=======================================");
        map.forEach((key,val)->{
            System.out.println("城市:"+key+" ---销售额: "+val);
        });
    }

    //分组计算每个城市总的销售额
    public static void testGroupBy2(){
        Map<String, Integer> map = list.stream().
                collect(Collectors.groupingBy(Employee::getCity, Collectors.summingInt(Employee::getSales)));
        log.info("testGroupBy输出:数据:{},大小:{}", map, map.size());
        map.forEach((key,value)->{
            System.out.println("城市:"+key+" ---销售额: "+ value);
        });
    }

    //分组计算每个城市的最高销售额
    public static void testGroupBy3(){
        Map<String, Employee> map = list.stream().collect(Collectors.groupingBy(Employee::getCity,
                Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Employee::getSales)), Optional::get)));
        map.forEach((key,val)->{
            System.out.println("城市:"+key+" 销售额最大员工:"+val);
        });
    }

    //去重,选择最后一个
    public static void testMap4(){
        Map<Long, Employee> map = list.stream().collect(Collectors.toMap(Employee::getId,o->o,(k1,k2)->k2));
        map.forEach((key,val)->{
            System.out.println("城市:"+key+" 销售额最大员工:"+val);
        });
    }

//获取最大值、最小值
public static void testReduce() { List<Integer> list1 = Arrays.asList(1,100,200); int sum = list1.stream().reduce(0,Integer::sum); int max = list1.stream().reduce(0,Integer::max); int min = list1.stream().reduce(0,Integer::min); log.info("FilterB sum输出:" + sum ); log.info("FilterB max输出:" + max ); log.info("FilterB min输出:" + min ); }