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

推荐订阅源

N
News and Events Feed by Topic
D
Docker
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
F
Full Disclosure
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
L
LangChain Blog
H
Help Net Security
B
Blog
T
Tailwind CSS Blog
V
V2EX
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
美团技术团队
A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
K
Kaspersky official blog
I
InfoQ
Project Zero
Project Zero
I
Intezer
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
Last Week in AI
Last Week in AI
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Securelist
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 叶小钗
S
Security Affairs
Blog — PlanetScale
Blog — PlanetScale
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
The Hacker News
The Hacker News

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