使用带可选的流时缺少return语句

时间:2018-01-12 20:04:53

标签: java lambda java-stream optional

return Optional.ofNullable(paymentContractData)
                .map(PaymentContractData::getPaymentPlan)
                .map(PaymentPlan::getActiveVersion)
                .map(PaymentPlanVersion::getPaymentInstruments)
                .ifPresent(instruments -> instruments
                        .stream()
                        .map(instrument -> {
                            PaymentInstrument curInstrument = new PaymentInstrument();
                            curInstrument.setPaymentInstrumentId(instrument.getExternalInstrumentId());
                            curInstrument
                                    .setPaymentInstrumentType(
                                            getConvertedPaymentMethodCode(instrument.getPaymentMethodCode()));

                        }***)
                        .collect(Collectors.toList()));
    }

有人可以帮我理解为什么我在星号标记位置得到编译错误缺少return语句。此外,如果用法是正确的,因为我需要在检查原始列表的来源是否为空之后返回格式化列表。

2 个答案:

答案 0 :(得分:2)

您在上次地图调用中省略了return语句:

return Optional.ofNullable(paymentContractData) 
     .map(PaymentContractData::getPaymentPlan)
     .map(PaymentPlan::getActiveVersion) 
     .map(PaymentPlanVersion::getPaymentInstruments)
     .ifPresent(instruments ->
         instruments.stream().map(instrument -> { 
            PaymentInstrument curInstrument = new PaymentInstrument();         
            curInstrument.setPaymentInstrumentId(instrument.getExternalInstrumentId());
            curInstrument.setPaymentInstrumentType( getConvertedPaymentMethodCode(instrument.getPaymentMethodCode())); 
            return curInstrument;
        }) 
    .collect(Collectors.toList())); }

答案 1 :(得分:1)

lambda语句“instrument - > {...}”需要将返回值映射到嵌入流中。

喜欢:

instrument -> { ...
                  return curInstrument; 
              }