用流列出bean

时间:2016-10-18 14:49:14

标签: java java-8 java-stream

我可以使用Java Stream API ???

重写以下代码
    List<ActionAttributeType> actionAttributeTypes = executeActionRsType.getBody().getActionAttributes();
    for (ActionAttributeType actionAttributeType : actionAttributeTypes) {
        if (actionAttributeType.getAttributeCode().equals("CONTRACTID")) {
            remoteBankingContractId = actionAttributeType.getAttributeValue();
            break;
        }
    }

1 个答案:

答案 0 :(得分:1)

这可以帮到你:

remoteBankingContractId = executeActionRsType.getBody().getActionAttributes().stream().filter(e -> "CONTRACTID".equals(e.getAttributeCode())).findFirst().orElse(null);

或者为您的变量类型获取Optional

Optional<TypeOfYourVariable> remoteBankingContractId = executeActionRsType.getBody().getActionAttributes().stream().filter(e -> "CONTRACTID".equals(e.getAttributeCode())).findFirst();