你好,我这里做错了

我想要获取基础ID,如果它不存在,则获取保险类型。代码片段粘贴在下面..但是我甚至在尝试了orElseGet()的orElse部分得到一个错误,它表示“目标类型Lambda必须是接口”。

String type = getFoundationId(companyInsurances).orElse(()->getInsuranceType(insurance, companyInsurances));




private Optional<String> getInsuranceType(Insurance insurance, List<CompanyInsurance> companyInsurances) {
    return InsurancePeriodHelper.findFirstCompanyInsuranceOfType(companyInsurances, CompanyInsuranceType.POLICY_HOLDER.getValue())
             .map(companyInsurance-> insurance.getProduct());
}

private Optional<String> getFoundationId(List<CompanyInsurance> companyInsurances) {
    return InsurancePeriodHelper.findFirstCompanyInsuranceOfType(companyInsurances, CompanyInsuranceType.FOUNDATION.getValue())
            .map(companyInsurance -> companyInsurance.getCompany().getFoundationIdentifier().toString());
}

最佳答案

在Java-9及更高版本中,您可以简单地使用Optional将此类Optional.or链接为:

String type = getFoundationId(companyInsurances)
        .or(() -> getInsuranceType(insurance, companyInsurances))
        .orElse("defaultValue");

07-24 22:12