本文介绍了java 8嵌套流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有这样的结构类:

Suppose you have structure classes like this:

public class Review{
    private Integer idReview;
    private String description;
    private ArrayList<RelReviewImage> images;
}

public class RelReviewImage{
    private Integer idRelReviewImage;
    private Integer idImage;
    private String name;
}

使用Java 8和 Stream s我们想为 idImage 做一个过滤器,并返回评论对象。

是有可能吗?一个级别很简单,但有两个级别我们找不到任何示例或文档。

With Java 8 and Streams we want to do a filter for idImage and return Review objects.
Is it possible? One level is easy, but 2 levels we can't find any example or documentation.

推荐答案

猜猜你需要什么:(假设getter可用于评论 RelReviewImage

Guess what you need: (Assume getters are available for Review and RelReviewImage)

List<Review> originalReviews = ...

List<Review> result = originalReviews.stream()
    .filter(review -> review.getImages().stream() //Nested streams. Assume getImages() never null, but empty
                          .anyMatch(image -> image.getIdImage() == 123)) //'2 level' here
    .collect(Collectors.toList());

这篇关于java 8嵌套流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:53