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

相关推荐
梦白.1 小时前
Python的容器类型
运维·python
沙漏无语3 小时前
(二)TIDB搭建正式集群
linux·数据库·tidb
tuotali20263 小时前
天然气压缩机技术2026,高可靠性长周期运行与智能运维融合路径
运维·python
思麟呀3 小时前
计算机网络初步认识
linux·计算机网络
姚不倒3 小时前
三节点 TiDB 集群部署与负载均衡搭建实战
运维·数据库·分布式·负载均衡·tidb
gpio_013 小时前
自建gitlab服务器并用sakurafrp穿透
运维·服务器·gitlab
k7Cx7e3 小时前
Debian执行ssh root@localhost 提示认证失败
运维·debian·ssh
浩子智控3 小时前
zynq嵌入式开发(2)—基本开发测试实例
linux·嵌入式硬件·硬件架构
闻道且行之3 小时前
Nginx 安装、做成服务及 HTTPS 配置全流程
linux·运维·nginx·https
爱上妖精的尾巴3 小时前
8-20 WPS JS宏 正则表达式-懒惰匹配
服务器·前端·javascript