关于Java8中lambda约简函数reduce的一个计算问题

   /**
     * testok
     * entity number->dto number reduce sum
     * 第二个dto需要在外层声明
     */
    @Test
    public void testDTOReduce(){

        LoansDTO resultLoansDTO = LoansDTO.builder().build();

        LoansDTO loansDTO = loansEntityList.stream()
                .map(loansEntity -> LoansDTO.builder()
                        .loanFirst(loansEntity.getLoanFirst())
                        .loanSecond(loansEntity.getLoanSecond())
                        .countSimle(loansEntity.getCountSimle()).build())
                .reduce((x, y) -> {
                    resultLoansDTO.setLoanFirst(x.getLoanFirst().add(y.getLoanFirst()));
                    resultLoansDTO.setLoanSecond(x.getLoanSecond().add(y.getLoanSecond()));
                    resultLoansDTO.setCountSimle(x.getCountSimle()+y.getCountSimle());
                    return resultLoansDTO;
                })
                .get();

        System.out.println(loansDTO);
    }

如上所示,第二个计算中DTO不能进行new 或者builder,而只是需要在外层进行先创建声明.否则无法获取结果,执行不到get()或者orElse()等

posted @ 2021-02-23 12:35  ukyo--君君小时候  阅读(443)  评论(0编辑  收藏  举报