zoukankan      html  css  js  c++  java
  • Stream将List转为Map汇总、排序

    Stream将List转换为Map,使用Collectors.toMap方法进行转换。

    背景:User类,类中分别有id,name,age三个属性。List集合,userList,存储User对象

    1、指定key-value,value是对象中的某个属性值

     Map<Integer,String> userMap1 = userList.stream().collect(Collectors.toMap(User::getId,User::getName));

    2、指定key-value,value是对象本身,User->User 是一个返回本身的lambda表达式

    Map<Integer,User> userMap2 = userList.stream().collect(Collectors.toMap(User::getId,User->User));

    3、指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身

     Map<Integer,User> userMap3 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));

    4、指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身,key 冲突的解决办法,这里选择第二个key覆盖第一个key

     Map<Integer,User> userMap4 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(key1,key2)->key2));

     进阶:https://blog.csdn.net/qq_39629277/article/details/83012548

    ======  按照容器状态排序、cellNo倒序==

    allCanOutStockList = stockDtotempList.stream()
    .filter(stockDto -> rpPickDContainerNoList.contains(stockDto.getContainerNo()))
    .sorted(Comparator.comparing(StockDtoTemp::getContainerStatus)
    .thenComparing(StockDtoTemp::getCanUseQty)
    .thenComparing(StockDtoTemp::getCellNo,Comparator.reverseOrder()))
    .collect(Collectors.toList());
    ==========================================================================           如果您觉得这篇文章对你有帮助,可以【关注我】或者【点赞】,希望我们一起在架构的路上,并肩齐行
    ==========================================================================
  • 相关阅读:
    seo 优化 仅针对 来拍呀www.laipaiya.com(一)
    mac 下 配置 xhprof
    mac 下 sphinx + mysql + php 实现全文搜索(xampp)(4)php api 解析
    mac 下 sphinx + mysql + php 实现全文搜索(xampp)(3)sphinx 的配置项解析
    php + mysql + sphinx 的全文检索(2)
    mac 下 sphinx + mysql + php 实现全文搜索(xampp)(1)
    mysql 的 存储结构(储存引擎)
    [php] yii debug设置
    [mysql] 查看mysql执行时间
    [javascript] 对象拷贝
  • 原文地址:https://www.cnblogs.com/amberJava/p/12427777.html
Copyright © 2011-2022 走看看