其他分享
首页 > 其他分享> > Map按日期(忽略时分秒精度 按yymmdd格式)分组求和 并排序 stream流实现

Map按日期(忽略时分秒精度 按yymmdd格式)分组求和 并排序 stream流实现

作者:互联网

// 在库量趋势
        LambdaQueryWrapper<StockInventoryDetailEntity> stayStockWrapper = new LambdaQueryWrapper<>();
        stayStockWrapper.eq(StockInventoryDetailEntity::getWarehouseCode, warehouseCode)
                .isNotNull(StockInventoryDetailEntity::getUpdateTime)
                .isNotNull(StockInventoryDetailEntity::getInventoryQuantity)
                .apply("DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(update_time)");
        List<StockInventoryDetailEntity> list = this.list(stayStockWrapper);
        Map<String, Double> stayStockTempMap = list.stream().collect(Collectors.groupingBy(o -> DateUtil.dateStr(o.getUpdateTime()),
                Collectors.summingDouble(StockInventoryDetailEntity::getInventoryQuantity)));
        // 排序
        LinkedHashMap<String, Double> stayStockMap = getStringDoubleLinkedHashMap(stayStockTempMap);
        stockTendDTO.setStayStockMap(stayStockMap);
/**
     * 排序
     *
     * @param stockTempMap
     * @return
     */
    private LinkedHashMap<String, Double> getStringDoubleLinkedHashMap(Map<String, Double> stockTempMap) {
        Object[] inStockKey = stockTempMap.keySet().toArray();
        Arrays.sort(inStockKey, Collections.reverseOrder());
        LinkedHashMap<String, Double> stockMap = new LinkedHashMap<>();
        for (int i = 0; i < inStockKey.length; i++) {
            stockMap.put(String.valueOf(inStockKey[i]), stockTempMap.get(inStockKey[i]));
        }
        return stockMap;
    }

 

标签:Map,stream,stockTempMap,stayStockWrapper,yymmdd,inStockKey,StockInventoryDetailE
来源: https://www.cnblogs.com/axibug/p/16113980.html