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

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
D
Docker
Vercel News
Vercel News
IT之家
IT之家
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
腾讯CDC
Google DeepMind News
Google DeepMind News
I
InfoQ
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
The GitHub Blog
The GitHub Blog
博客园 - Franky
The Cloudflare Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
T
Tenable Blog
P
Proofpoint News Feed
Recorded Future
Recorded Future
Security Latest
Security Latest
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 聂微东
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google Online Security Blog
Google Online Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
TaoSecurity Blog
TaoSecurity Blog
U
Unit 42
The Hacker News
The Hacker News
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
F
Full Disclosure
M
MIT News - Artificial intelligence
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
Project Zero
Project Zero

博客园 - 大山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 ); }