首页 > 数据库技术 > 详细

巧用Stream:像sql一样操作数据集合

时间:2020-05-28 21:50:33      阅读:52      评论:0      收藏:0      [点我收藏+]

背景:

  从数据库中查询出的数据,最各种运算。Stream最方便。 

//转Map操作 实体中的2个字段,转为key=字段 value=字段
Map<String, Integer> collect = studentList.stream().collect(Collectors.toMap(Student::getGroupPointId, Student::getGroupStatus));

//转Map操作 key=某个字段,value=实体对象
Map<String, Student> studentDBMap = studentList.stream().collect(Collectors.toMap(Student::getGroupNo,v->v,(o,n)->n));

//分组统计,key-List
Map<Long, List<MapDic>> dicMap = mapDics.stream().collect(groupingBy(m -> m.getMapAreaId()));

 

//分组统计
Map<String, Long> storageCountMap = request.getStorageList().stream().collect(Collectors.groupingBy(Student::getAge, Collectors.counting()));

//多级分组 统计
Map<Long,Map<String,List<Student>>> map = points.stream().collect(groupingBy(e->e.getMapAreaId(),
groupingBy(e -> e.getLogicalCode()+"_"+e.getLogicalName())));

//转list
List<Stream> itemList = studentStreams.stream().map(this::getStreamObj).collect(Collectors.toList());
List<String> alleyNoStrList = alleyList.stream().map(Student::getAge).collect(Collectors.toList());

 

巧用Stream:像sql一样操作数据集合

原文:https://www.cnblogs.com/amberJava/p/12983562.html

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