steam流式操作

时间:2021-09-03
本文章向大家介绍steam流式操作,主要包括steam流式操作使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

jdk1.8后开始支持的stream流,最近用了流之后,都快不想写for循环了,流写起来实在是太方便了。

这里记录下我平时常用的几个操作:

List<Map<String,  Object>> 根据map中key为value的值进行排序

list.sort(Comparator.comparingLong(m -> Long.parseLong(m.get("value").toString())));

List<实体>转List<String>

List<String> strList = list.stream().map(实体::getId).collect(Collectors.toList())

List<Long>转 Map<Long, Long>

Map<Long, Long> map = list.stream().collect(Collectors.toMap(Function.identity(), Function.identity()));

List<Map<String,  Object>>转Map

Map map = list.stream().collect(Collectors.toMap(m -> m.get("index"), m -> m.get("value")));

List<Map<String,  Object>>转Set

Set set = list.stream().map(m -> m.get("value")).collect(Collectors.toSet());
有疑问 可以私信 所有的私信我都会看的 如文章有什么专业上的错误 欢迎评论和私信 大家一起共同进步

原文地址:https://www.cnblogs.com/half-moon-stars/p/15155960.html