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

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - npe0

策略模式在项目中的应用 博文阅读密码验证 - 博客园 MyBatis-Plus 的动态SQL片段用法 EasyExcel的多级表头 docker镜像搬运命令 macbook绕过安全控制安装第三方软件 Python中使用列表、map和filter函数配合lambda表达式来操作集合 阿里通义千问大模型初探 java.io.IOException: Cannot run program “az”: CreateProcess error=2, 系统找不到指定的文件。 Git使用问题记录 seata的分布式事务处理机制 JVM常用命令 easyexcel导出Bigdecimal数据格式问题 百万千万级Excel导出 arthas 常用命令 对find,xargs,grep和管道的一些深入理解 PlantUML 分库分表 jvm优化
你可能不知道的Collectors用法
npe0 · 2023-03-27 · via 博客园 - npe0

1.0、初始化数据

伪代码

User a1 = User.builder().age(2).name("A").build();
User a = User.builder().age(1).name("A").build();
User b = User.builder().age(2).name("B").build();
User c = User.builder().age(3).name("C").build();
User d = User.builder().age(4).name("D").build();

List<User> userList = Lists.newArrayList(a1,a,b, c, d);

User flatUser1 = User.builder().age(4).name("as,df,gh").build();
User flatUser2 = User.builder().age(4).name("gh,jk").build();
List<User> flatUserList = Lists.newArrayList(flatUser1,flatUser2);

1.1、toMap

key值相同时的策略

//k1和k2重复,返回k2
Map<String, User> collectK2 = userList.stream().collect(Collectors.toMap(User::getName, Function.identity(), (k1, k2) -> k2));
//按照自定义条件返回key,例如按照最大age返回key
Map<String, User> collectMax = userList.stream().collect(Collectors.toMap(User::getName, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(User::getAge))));

1.2、flatMap

map 的输入和输出是 1:1

flatMap的输入和输出是 1:N

//map 的输入和输出是 1:1
//如果直接用map取值,则输出 [[Ljava.lang.String;@2a2d45ba, [Ljava.lang.String;@2a5ca609]
flatUserList.stream().map(var -> var.getName().split(",")).collect(Collectors.toList());
//使用flatMap 则输出 [as, df, gh, jk]
//flatMap的输入和输出是 1:N
flatUserList.stream().map(var->var.getName().split(",")).flatMap(Arrays::stream).distinct().collect(Collectors.toList());

1.3、groupingBy

分组策略-分组字段重复时取值逻辑

//默认value是list
userList.stream().collect(Collectors.groupingBy(User::getName));
//指定value返回类型
userList.stream().collect(Collectors.groupingBy(User::getName,Collectors.toSet()));
//分组策略-保留age最大的那一条
userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.maxBy(Comparator.comparing(User::getAge))));
//分组策略-保留age最小的那一条
userList.stream().collect(Collectors.groupingBy(User::getName, Collectors.minBy(Comparator.comparing(User::getAge))));

1.4、collectingAndThen

对象集合去重

//name属性去重
userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new));
//name和age去重
userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->new TreeSet<>(Comparator.comparing(user -> user.getName()+ "-" + user.getAge()))), ArrayList::new));

1.5、maxBy/minBy

取最大最小值

// 最大、最小age
userList.stream().collect(Collectors.maxBy(Comparator.comparing(User::getAge)));
userList.stream().collect(Collectors.minBy(Comparator.comparing(User::getAge)));

1.6、reduce

累加求和

// reduce、reducing、summingInt 效果一样
userList.stream().map(User::getAge).reduce(0, (x, y) -> x + y));
userList.stream().map(User::getAge).collect(Collectors.reducing(0,Integer::sum));
userList.stream().map(User::getAge).collect(Collectors.summingInt(Integer::intValue));