zoukankan      html  css  js  c++  java
  • Stream流分组,统计,求和

    public class Test {
    
    public static void main(String[] args) {
    List<OrdersDO> list = new ArrayList<>();//查询昨天一天的所有交易
    OrdersDO o1 = new OrdersDO();
    o1.setAppId(1L);
    o1.setTradeAmount(100L);
    o1.setStatus(1);
    list.add(o1);
    OrdersDO o2 = new OrdersDO();
    o2.setAppId(5L);
    o2.setTradeAmount(300L);
    o2.setStatus(2);
    list.add(o2);
    OrdersDO o3 = new OrdersDO();
    o3.setAppId(1L);
    o3.setTradeAmount(100L);
    o3.setStatus(3);
    list.add(o3);
    OrdersDO o4 = new OrdersDO();
    o4.setAppId(5L);
    o4.setTradeAmount(300L);
    o4.setStatus(4);
    list.add(o4);
    OrdersDO o5 = new OrdersDO();
    o5.setAppId(5L);
    o5.setTradeAmount(300L);
    o5.setStatus(4);
    list.add(o5);
    //统计每个应用实际支付总额
    Map<Long, Long> tradeAmountMap = list.stream().filter(o->o.getStatus()==2)
    .collect(Collectors.groupingBy(OrdersDO::getAppId,
    Collectors.summingLong(OrdersDO::getTradeAmount)));
    System.out.println(tradeAmountMap);
    
    //统计每个应用取消总额
    Map<Long, Long> cancelAmountMap = list.stream()
    .collect(Collectors.groupingBy(OrdersDO::getAppId,
    Collectors.summingLong(OrdersDO::getTradeAmount)));
    System.out.println(cancelAmountMap);
    
    //统计每个应用下交易笔数
    Map<Long, Long> appTradeNum = list.stream().collect(Collectors.groupingBy(OrdersDO::getAppId, Collectors.counting()));
    System.out.println(appTradeNum);
    
    //统计每个应用每种状态下交易笔数
    Map<Long, Map<Integer, Long>> tradeNumMap = list.stream().
    collect(Collectors.groupingBy(OrdersDO::getAppId,
    Collectors.groupingBy(OrdersDO::getStatus,
    Collectors.counting())));
    System.out.println(tradeNumMap);
    
    //每个应用下交易笔数按数量排序
    Map<Long,Long> finalMap = new LinkedHashMap<>();
    appTradeNum.entrySet().stream().sorted(Map.Entry.<Long, Long>comparingByValue().reversed()).forEachOrdered(e->finalMap.put(e.getKey(),e.getValue()));
    System.out.println(finalMap);
    }
    }
  • 相关阅读:
    QML中的states用法
    mako的namespace(四)
    Dynamics CRM2013 按钮自定义开发工具 Ribbon Workbench 介绍
    CRM 导入导出附件上传大小限制控制
    插件(plugin)调试技巧及注意事项
    Dynamics CRM2013 自定义开发工具 Dynamics XRM Tools 介绍
    传递正能量,拒绝负能量!加油
    让人无语的编程环境
    转NET(C#):线程本地存储(ThreadLocal Storage)之ThreadStatic, LocalDataStoreSlot和ThreadLocal<T>
    转javaThreadLocal
  • 原文地址:https://www.cnblogs.com/tubashu/p/11994535.html
Copyright © 2011-2022 走看看