如何从对象流中统一对象的集合

问题描述 投票:0回答:2

我有这个 CasaDeBurrito 课程:

public class CasaDeBurritoImpl implements OOP.Provided.CasaDeBurrito {

    private Integer id;
    private String name;
    private Integer dist;
    private Set<String> menu;
    private Map<Integer, Integer> ratings;
...
}

还有这个教授课:(应该带一个s)

public class ProfessorImpl implements OOP.Provided.Profesor {

    private Integer id;
    private String name;
    private List<CasaDeBurrito> favorites;
    private Set<Profesor> friends;

    private Comparator<CasaDeBurrito> ratingComparator = (CasaDeBurrito c1, CasaDeBurrito c2) ->
    {
        if (c1.averageRating() == c2.averageRating()) {
            if (c1.distance() == c2.distance()) {
                return Integer.compare(c1.getId(), c2.getId());
            }
            return Integer.compare(c1.distance(), c2.distance());
        }
        return Double.compare(c2.averageRating(), c1.averageRating());
    };

    private Predicate<CasaDeBurrito> isAvgRatingAbove(int rLimit) {
        return c -> c.averageRating() >= rLimit;
    };

    public Collection<CasaDeBurrito> 
    filterAndSortFavorites(Comparator<CasaDeBurrito> comp, Predicate<CasaDeBurrito> p) {
            return favorites.stream().filter(p).sorted(comp).collect(Collectors.toList());
    }

    public Collection<CasaDeBurrito> favoritesByRating(int rLimit) {
            return filterAndSortFavorites(ratingComparator, isAvgRatingAbove(rLimit));
    }
}

我想实现一个函数,它获取一个

Profesor prof
,并将所有
favorites
的朋友的所有
prof
集合(按ID排序)与流统一起来。 因此,我想要按评级收集所有最喜欢的
CasaDeBurrito
餐厅(通过 favoritesByRating)。

例如:

public Collection<CasaDeBurrito> favoritesByRating(Profesor p) {

    Stream ret = p.getFriends().stream()
               .<*some Intermediate Operations*>.
               .forEach(y->y.concat(y.favoritesByRating(0))
               .<*some Intermediate Operations*>.
               .collect(toList());
    return ret;
}
java collections java-stream flatten
2个回答
1
投票

你想要一个

a collection of all CasaDeBurrito favorites by friends sorted by name
所以我想说
Map<String, List<CasaDeBurrito>>
就是你所需要的,每个键都是一个朋友的名字,值是他喜欢使用你的
CasaDeBurrito
方法的
favoritesByRating
列表,全部排序姓名(使用
TreeMap

public Map<String, List<CasaDeBurrito>> favoritesByRating(Profesor p) {
    return p.getFriends().stream()
            .collect(toMap(Profesor::getName, prof -> prof.favoritesByRating(0), (i, j) -> i, TreeMap::new));
}

如果您只想列出朋友喜欢的

CasaDeBurrito
,请使用
flatMap

public List<CasaDeBurrito> favoritesByRating(Profesor p) {
    return p.getFriends().stream()
            .flatMap(prof -> prof.favoritesByRating(0).stream())
            .collect(toList());
}

0
投票

我设法将评论和答案与我需要的内容联系起来。 我按 ID 对朋友进行排序,并创建了一个集合流,如 @Alex Faster 在他的评论此处中分享,并按照上面的建议压平该流。

return p.getFriends().stream()  //Stream<Profesor>
                .sorted(Comparator.comparingInt(Profesor::getId)) //Stream<Profesor>
                .map(P->P.favoritesByDist(0)) //Stream<Collection<CasaDeBurrito>>
                .flatMap(Collection::stream) //Stream<CasaDeBurrito>
                .distinct()
                .collect(Collectors.toList()); //List<CasaDeBurrito>
© www.soinside.com 2019 - 2024. All rights reserved.