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());

相关推荐
ybb_ymm30 分钟前
mysql8在linux下的默认规则修改
linux·运维·数据库·mysql
半梦半醒*32 分钟前
zabbix安装
linux·运维·前端·网络·zabbix
Panda__Panda2 小时前
docker项目打包演示项目(数字排序服务)
运维·javascript·python·docker·容器·c#
xx.ii2 小时前
Docker监控
运维·docker·容器
板鸭〈小号〉2 小时前
Socket网络编程(2)-command_server
运维·服务器
维尔切2 小时前
Docker 存储与数据共享
运维·docker·容器
温柔一只鬼.2 小时前
Docker快速入门——第四章Docker镜像
运维·docker·容器
温柔一只鬼.3 小时前
Docker快速入门——Windowns系统下Docker安装(2025最新理解与完整,附带WSL1如何升级为WSL2)
运维·docker·容器
武文斌773 小时前
复习总结最终版:单片机
linux·单片机·嵌入式硬件·学习
驱动探索者3 小时前
贝尔实验室发展史:20世纪科技圣殿的辉煌与沉浮
linux