Lambda表达式常用场景

1、集合变字符串

如果你的集合里泛型是List<String>,那么可以直接用String.join(",",你的集合),把它变为字符串。

String.join(",", yourList)

但是如果你的集合是,List<Integer>、List<Long>,那么String.join这个方法就不适应了.

你可以用lamba表达式

String string= longs.stream().map(Object::toString).collect(Collectors.joining(","));

2、遍历Map

方法一 :

//entrySet 键值对

Set<Map.Entry<String, List<Integer>>> entrySet = test.entrySet();

List<Integer>integersEnd = new ArrayList<>();

for (Map.Entry<String, List<Integer>> stringListEntry : entrySet) {

integersEnd.add(stringListEntry.getValue().get(0));

}

方法二(用lamba表达式):

List<Integer> collect = test.entrySet().stream().map(m -> m.getValue().get(0)).collect(Collectors.toList());

3、集合去重

List<String> distinctSeriesName = stringList.stream().distinct().collect(Collectors.toList());

相关推荐
uiop_uiop_uiop2 小时前
fnOS LUKS on RAID Storage Pool
服务器
IT大白鼠2 小时前
Linux进程与计划任务管理:技术详解与实战指南
linux·运维·服务器
拾贰_C2 小时前
【Ubuntu | 公共工作站 | mysql 】 MySQL残留物残留数据
linux·mysql·ubuntu
临街的小孩2 小时前
Docker 容器内运行 ROS Noetic 图形界面(rqt_image_view)极简教程总结
运维·docker·容器
Ujimatsu2 小时前
虚拟机安装Ubuntu 26.04.x服务器版(命令行版)(2026.5)
linux·windows·ubuntu
hweiyu003 小时前
Linux命令:arptables
linux·运维
看到代码头都是大的4 小时前
Windows环境下绿色版nginx 1.30使用
运维·nginx
IT研究所4 小时前
AI 时代下的知识管理:从 Claude 的“复盘”能力看生成式 AI价值
大数据·运维·数据库·人工智能·科技·低代码·自然语言处理
仙柒4154 小时前
管理网络安全
linux·运维·服务器