首页 > 其他 > 详细

steam流式操作

时间:2021-09-04 00:15:41      阅读:25      评论:0      收藏:0      [点我收藏+]

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(实体::getValue).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());

 

steam流式操作

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!