当前位置:网站首页>常用lambda表达式
常用lambda表达式
2022-08-03 11:42:00 【飞四海】
交集 并集 差集
交集(listA ∩ ListB):
List<Person> listC = listA.stream().filter(item -> listB.contains(item)).collect(Collectors.toList());
listC中的元素有:属性name值为 aa, bb, cc 的对象。
并集(listA ∪ listB):
//先合体
listA.addAll(listB);
//再去重
List<Person> listC = listA.stream().distinct().collect(Collectors.toList());
listC中的元素有:属性name值为 aa, bb, cc ,dd的对象。
差集(listA - listB):
List<Person> listC = listA.stream().filter(item -> !listB.contains(item)).collect(Collectors.toList());
listC中的元素有:属性name值为 dd的对象。
List集合的过滤之lambda表达式
stmtList = stmtList.stream().filter(stmt -> !(stmt instanceof OracleInsertStatement) && stmt.getParent() == null).collect(Collectors.toList());
lambda表达式将List对象某个字段转换以逗号分隔的String类型
// 获取 users 集合中的 id 集合
List<Long> ids = users.stream().map(User::getId).collect(Collectors.toList());
System.out.println("ids: " + ids);
System.out.println();
// 获取 users 集合中的 id 集合并转为字符串, 通过 , 拼接p
String idsTxt = users.stream().map(User::getId).map(String::valueOf).collect(Collectors.joining(","));
System.out.println("idsTxt: " + idsTxt);
System.out.println();
List<String> names = users.stream().map(User::getName).collect(Collectors.toList());
System.out.println("names: " + names);
边栏推荐
- LeetCode 899 Ordered queue [lexicographical order] HERODING's LeetCode road
- 使用.NET简单实现一个Redis的高性能克隆版(一)
- LyScript 实现对内存堆栈扫描
- LeetCode-1796. 字符串中第二大的数字
- MySQL之json数据操作
- Traceback (most recent call last): File
- [Detailed explanation of binary search plus recursive writing method] with all the code
- Generate interface documentation online
- 小身材有大作用——光模块基础知识(一)
- LeetCode-142. 环形链表 II
猜你喜欢

FR9811S6 SOT-23-6 23V,2A同步降压DC/DC转换器

mysql进阶(二十四)防御SQL注入的方法总结

性能优化|从ping延时看CPU电源管理

赛灵思MPSOC裸机下的 USB调试实验

基于PHP7.2+MySQL5.7的回收租凭系统

fast planner中拓扑路径搜索

The effects of the background and the Activiti

fastposter v2.9.0 programmer must-have poster generator

mysql advanced (twenty-four) method summary of defense against SQL injection

c语言进阶篇:内存函数
随机推荐
The effects of the background and the Activiti
Lease recovery system based on PHP7.2+MySQL5.7
矩阵的计算[通俗易懂]
What is the ERC20 token standard?
云原生 Dev0ps 实践
c语言进阶篇:内存函数
增加WebView对localStorage的支持
2022年五面蚂蚁、三面拼多多、字节跳动最终拿offer入职拼多多
mysql进阶(二十四)防御SQL注入的方法总结
【MySQL】数据库进阶之索引内容详解(上篇 索引分类与操作)
字符串本地化和消息字典(二)
Android 技术面试准备(含面试题及答案)
FE主导打造一个运营活动平台
什么是Weex
LeetCode——1161. 最大层内元素和
下午见!2022京东云数据库新品发布会
详解虚拟机!京东大佬出品HotSpot VM源码剖析笔记(附完整源码)
字节最爱问的智力题,你会几道?
LeetCode 899 有序队列[字典序] HERODING的LeetCode之路
[论文阅读] (23)恶意代码作者溯源(去匿名化)经典论文阅读:二进制和源代码对比