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

相关推荐
Lolo_fi2 分钟前
记录Fedora43上安装向日葵
linux
noravinsc9 分钟前
两台 centos 7.9 部署 pbs version 18.1.4 集群
linux·运维·centos
你的微笑,乱了夏天11 分钟前
linux centos常用命令整理
linux·运维·centos
故林丶16 分钟前
【Linux】CentOS 7.8 Docker + Docker Compose 安装
linux·docker·centos
刘一说17 分钟前
CentOS系统一键安装Memcached教程
linux·centos·memcached
网硕互联的小客服1 小时前
Windows2008 如何禁用FSO?
运维·服务器·网络·windows·安全
不惑_1 小时前
[特殊字符] 在 Linux 上设置 SQLite
linux·jvm·sqlite
上线就吃代码1 小时前
【等保测评】数据库数据库配置have_ssl参数为yes
服务器·数据库·ssl
塔能物联运维1 小时前
物联网运维中的多模态数据融合与智能决策优化技术
运维·物联网
xingyue_S1 小时前
LVS负载均衡群集(一) -- NAT模式
运维·负载均衡·lvs