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

相关推荐
獭.獭.24 分钟前
Linux -- 信号【上】
linux·运维·服务器
hashiqimiya27 分钟前
centos配置环境变量jdk
linux·运维·centos
hashiqimiya28 分钟前
权限更改centos中系统文件无法创建文件夹,使用命令让普通用户具备操作文件夹
linux
路由侠内网穿透2 小时前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
傻傻虎虎4 小时前
【Docker】常用帮忙、镜像、容器、其他命令合集(2)
运维·docker·容器
ZERO_pan4 小时前
服务器装机遇到的问题
运维·服务器
逆小舟4 小时前
【Linux】人事档案——用户及组管理
linux·c++
青草地溪水旁4 小时前
pthread_mutex_lock函数深度解析
linux·多线程·pthread
l1t5 小时前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
杀气丶5 小时前
Linux下运行芙蕾雅天堂2【俄文简译L2FATER】
运维·服务器·天堂2·l2fater·l2fater.cn