zoukankan      html  css  js  c++  java
  • stream常用方法记录

      与数据的操作尽量使用批量,这个时候,就出现了很多List,对list的操作,使用stream特别方便,但是有时候,一些方法还是没记住,这里针对常用的方法做一个统计,这篇文档持续更新,将遇到的都记录好。

      方法千万种,这里记录一种。

    1.聚合

      可以将list下的每个对象的contractId给过滤出来,然后形成一个list结果。

    contractIdList = billTermList.stream().map(ContractBillTermMonitorDetailDTO::getContractId).collect(Collectors.toList());
    

      

    2.分组

      对list中的对象进行分组

    Map<Long, List<KasEveryDayBillDataRespDTO>> listMap = contractBillData.stream().collect(Collectors.groupingBy(KasEveryDayBillDataRespDTO::getContractId));
    

     

    3.排序

    List<ContractBillTermRecordDTO> sortedHistoryList = historyList.stream().sorted(Comparator.comparing(ContractBillTermRecordDTO::getBillStartTime)).collect(Collectors.toList());
    

      

    4.倒排

    List<ContractBillTermRecordDTO> sortedBillTermRecordList = contractBillTermRecordDTOList.stream().sorted(Comparator.comparing(ContractBillTermRecordDTO::getBillStartTime).reversed())
                        .collect(Collectors.toList());
    

      

    5.对Integer字段进行相加

    laterMonthBillData = laterList.stream().mapToInt(ContractBillTermRecordDTO::getAmount).sum();
    

      

    6.计算list的个数

    int tmp = (int) sortedHistoryList.stream().filter(sortedHistory -> currentBillTermRecordDTO.getBillStartTime().isBefore(sortedHistory.getBillStartTime())).count();
    

      

    7.BigDecimal的相加

    contractOverpaidMoneyRecordDTOList.stream().map(ContractOverpaidMoneyRecordDTO::getHasDeductOverpaidAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
    

      

  • 相关阅读:
    IE CSS Bug及解决方案参考手册
    如何学习Javascript
    JS获取当前对象大小以及屏幕分辨率等
    自适应网页设计的方法
    【Javascript Trick系列】二、table相关操作
    移动web开发经验总结
    兼容各种浏览器的常用按钮样式
    获取浏览器的高度和宽度
    让页面弹框垂直水平居中
    Vue is used in projects
  • 原文地址:https://www.cnblogs.com/juncaoit/p/12527797.html
Copyright © 2011-2022 走看看