zoukankan      html  css  js  c++  java
  • 巧用Stream:像sql一样操作数据集合

    背景:

      从数据库中查询出的数据,最各种运算。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());

    ==========================================================================           如果您觉得这篇文章对你有帮助,可以【关注我】或者【点赞】,希望我们一起在架构的路上,并肩齐行
    ==========================================================================
  • 相关阅读:
    模拟展示动态按钮
    模拟界面请求到web服务器
    bean的生命周期
    structs2的action实现方式
    annotation中的Autowired
    华为笔试题练习
    开发工具
    [转]Linux双向链表的知识
    【转】 嵌入式C语言编程中Inline函数的应用
    打印格式化printf
  • 原文地址:https://www.cnblogs.com/amberJava/p/12983562.html
Copyright © 2011-2022 走看看