惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - 【当耐特】
月光博客
月光博客
Vercel News
Vercel News
D
Docker
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 叶小钗
MongoDB | Blog
MongoDB | Blog
GbyAI
GbyAI
有赞技术团队
有赞技术团队
雷峰网
雷峰网
博客园 - 聂微东
小众软件
小众软件
Y
Y Combinator Blog
腾讯CDC
L
LangChain Blog
The GitHub Blog
The GitHub Blog
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss

博客园 - 大山008

若依导出excel时decimal数据千分位格式化 SpringBoot如何引入deepseek-多轮对话 EasyExcel实现百万级数据导入导出 SpringAMQP整合RabbitMQ使用 win11安装redis步骤详解 ftp与sftp工具类 子类的toString方法如何打印父类的属性 MySQL常见的几种优化方案 关于若依AsyncFactory的一些思考,记录一下 Vue 路由跳转、路由传参、跳转区别、传值取值 MYSQL中substring_index()用法 mybatis sql 解决 in 参数过多的问题 MySQL之json数据操作 mybatis查询返回map键值对的问题 validate校验,记录一种思路 在vue中用multipart/form-data方式上传文件并同表单同步提交数据 Java微信转发及网络检测 生成带有二维码的海报 java将指定文件夹下面的所有图片压缩到指定大小以内并保存图片 Lock与ReentrantLock、Synchronized
stream流的一些常用用法
大山008 · 2023-06-30 · via 博客园 - 大山008
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Employee {
    private Long id;
    private String name;   //
    private String city;   // 城市
    private Integer sales;  // 销售额
    private boolean status;  // 销售额
}
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 ); }