使用 Java 8 过滤对象列表并将过滤结果映射回同一列表

我有一个包含对象列表的列表,我想从列表中仅检索活动记录。下面是我正在尝试的代码,但它没有正确过滤。


 private List<SkuList> getActiveSkuListsAndSkuListLineItem(List<SkuListEntity> skuListEntityList) {

return skuListEntityList.stream()

    .filter(x -> x.getStatusTypDbky().getTypDbkey().equals(SkuListStatus.ACTIVE.getId()))

    .map(x -> x.getSkuListLineItems())

    .flatMap(x -> x.stream()

        .filter(s -> s.getStatusTypDbky().getTypDbkey().equals(SkuListLineItemStatus.ACTIVE.getId())))

    .map(x -> x.getSkuListEntity())

    .collect(Collectors.toList())

    .stream().map(x -> skuListMapper.toSkuListResource(x)).collect(Collectors.toList());

}

上面的代码正在检索所有记录,无论过滤器 (ACTIVE) 条件如何。如果我破坏上面的代码,如下所示:


skuListEntityList = skuListEntityList.stream()

        .filter(x -> x.getStatusTypDbky().getTypDbkey().equals(SkuListStatus.ACTIVE.getId()))

        .collect(Collectors.toList());


 List<SkuListLineItemEntity> skuListLineItemEntityList = skuListEntityList.stream()

    .flatMap(x -> x.getSkuListLineItems().stream()

        .filter(s -> s.getStatusTypDbky().getTypDbkey().equals(SkuListLineItemStatus.ACTIVE.getId())))

    .collect(Collectors.toList());

那么skuListEntityList对象skuListLineItemEntityList只包含活动记录。当我将逻辑合并到一个 return 语句中时,我在上面做的事情出了什么问题。


皈依舞
浏览 131回答 1
1回答

FFIVE

如果没有实际的类,很难测试您的代码,但是您的第一个流中会添加很多噪音。下面是您的原始代码,其中添加了注释,希望能够消除您添加的一些额外噪音。return skuListEntityList.stream()&nbsp; &nbsp; .filter(x -> x.getStatusTypDbky().getTypDbkey().equals(SkuListStatus.ACTIVE.getId()))&nbsp; &nbsp; .map(x -> x.getSkuListLineItems())&nbsp; &nbsp; .flatMap(x -> x.stream()&nbsp; &nbsp; &nbsp; &nbsp; .filter(s -> s.getStatusTypDbky().getTypDbkey().equals(SkuListLineItemStatus.ACTIVE.getId())))&nbsp; &nbsp; .map(x -> x.getSkuListEntity())//remove and simply operate on x below&nbsp; &nbsp; .collect(Collectors.toList())//This is not needed as you're already working with a stream&nbsp; &nbsp; .stream()//This is not needed as you're already working with a stream&nbsp; &nbsp; .map(x -> skuListMapper.toSkuListResource(x/*x.getSkuListEntity() remove map above*/)).collect(Collectors.toList());你的答案应该看起来更符合这个思路。尽管没有充实的类,我不能保证您的逻辑按照您的预期工作。&nbsp; &nbsp; &nbsp; &nbsp; return skuListEntityList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(x -> x.getStatusTypDbky().getTypDbkey().equals(SkuListStatus.ACTIVE.getId()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(x -> x.getSkuListLineItems().stream())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(s -> s.getStatusTypDbky().getTypDbkey().equals(SkuListLineItemStatus.ACTIVE.getId()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(x -> skuListMapper.toSkuListResource(x.getSkuListEntity()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());我建议阅读 https://www.baeldung.com/java-difference-map-and-flatmap 和 https://docs.oracle.com/javase/8/docs/api/java/util/stream/package- summary.html 准确了解您在每个中间操作中所做的事情。如果您对到底发生了什么感到困惑,请删除方法链接并使用调试器查看调用的实际返回值。IE。Stream<SkuListEntity> skuListEntityStream = skuListEntityList.stream();Stream<SkuListEntity> filteredSkuListEntityStream = skuListEntityStream.filter();...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java