Java8 stream 流 获取集合中的某字段的求和最大最小等操作

1.如图:

Java8 stream 流 获取集合中的某字段的求和最大最小等操作_第1张图片

2.代码:

@Override
    public SmsMarketingTotalVO getSmsMarketingTotal(SmsMarketingQuery query) {
        List list = this.smsMarketingMapper.findSmsMarketingTotalListByQuery(query);
        SmsMarketingTotalVO smsMarketingTotalVO = new SmsMarketingTotalVO();
        /* 批次数量 */
        smsMarketingTotalVO.setBatchTotal((long)list.size());
        /*客户总数*/
        smsMarketingTotalVO.setClientTotal(list.stream().mapToLong(t -> t.getClientTotal() == null ? 0 :t.getClientTotal()).sum());
        /*  进件数量 */
        smsMarketingTotalVO.setPurchaseCount(list.stream().mapToInt(t -> t.getPurchaseCount() == null ? 0 :t.getPurchaseCount()).m());
        /*  放款数量 */
        smsMarketingTotalVO.setLoanCount(list.stream().mapToInt(t -> t.getLoanCount() == null ? 0 :t.getLoanCount()).sum());
        /* 放款率 百分比 */
        BigDecimal loanCount = BigDecimalUtil.getBigDecimal(smsMarketingTotalVO.getLoanCount().toString());
        BigDecimal clientTotal = BigDecimalUtil.getBigDecimal(smsMarketingTotalVO.getClientTotal().toString());
        if(smsMarketingTotalVO.getClientTotal() > 0){
            BigDecimal multiply = loanCount.divide(clientTotal,4, RoundingMode.HALF_UP);
            smsMarketingTotalVO.setLoanRate((BigDecimalUtil.getBigDecimal(multiply.doubleValue() * 100+"").setScale(2, BigDecimal.ROUND_DOWN).doubleValue())+"%");
        }else {
            smsMarketingTotalVO.setLoanRate("--");
        }
        return smsMarketingTotalVO;
    }

 

你可能感兴趣的:(Java后端代码)