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

相关推荐
Yogurt_cry4 小时前
[树莓派4B] 闲置近10年的爱普生 L310 打印机爆改无线打印机
linux·物联网·树莓派
爱吃橘子橙子柚子1 天前
3CPU性能排查总结(超详细)【Linux性能优化】
运维·cpu
Johny_Zhao1 天前
OpenClaw中级到高级教程
linux·人工智能·信息安全·kubernetes·云计算·yum源·系统运维·openclaw
Sheffield2 天前
Docker的跨主机服务与其对应的优缺点
linux·网络协议·docker
Sheffield2 天前
Alpine是什么,为什么是Docker首选?
linux·docker·容器
舒一笑3 天前
程序员效率神器:一文掌握 tmux(服务器开发必备工具)
运维·后端·程序员
Johny_Zhao3 天前
centos7安装部署openclaw
linux·人工智能·信息安全·云计算·yum源·系统运维·openclaw
haibindev3 天前
在 Windows+WSL2 上部署 OpenClaw AI员工的实践与踩坑
linux·wsl2·openclaw
NineData3 天前
数据库管理工具NineData,一年进化成为数万+开发者的首选数据库工具?
运维·数据结构·数据库