如何使用java 8流收集一个列表中的所有实例

时间:2016-10-28 08:49:12

标签: collections java-8 java-stream

我有一个班级

public class RegulatoryApproval {

    private String approvalType;
    private Boolean deleted;
    private RegulatorCategory regulatorCategory;
    //getters and setters

}

另一个班级

public class RegulatorCategory {
    ...
    private List<CreditReportingField> creditReportingFields;
    //getters and setters
}

我收到了RegulatoryApproval列表。我想在一个列表中收集RegulatorCategory中的所有CreditReportingFields。我正在尝试,但我仍然坚持如何做到这一点。

我收到错误

Type mismatch: cannot convert from List<Stream<CreditReportingField>> to List<CreditReportingField>

这就是我在做什么

List<CreditReportingField> creditReportingFields = null;

List<RegulatoryApproval> regulatoryApprovals = regulatoryApprovalRepository.findDistinctByIdInAndDeletedFalse(courseApprovalIds);
    if (!CollectionUtils.isEmpty(regulatoryApprovals)) {
        creditReportingFields = regulatoryApprovals
        .stream()
        .map(RegulatoryApproval::getRegulatorCategory)
        .filter(p -> p!= null)
        .map(RegulatorCategory::getCreditReportingFields)
        .filter(p -> !CollectionUtils.isEmpty(p))
        .map(p -> p.
                stream()
                .filter(creditReportingField -> creditReportingField != null)
                //.collect(Collectors.toList())
        )
        .collect(Collectors.toList());
    }

我该如何收集它?这种方法是正确的还是有更好的方法来做到这一点?

由于

0 个答案:

没有答案