Java 如何统一对象';从对象流中删除集合

Java 如何统一对象';从对象流中删除集合,java,collections,java-stream,flatten,Java,Collections,Java Stream,Flatten,我有一个卡萨迪玉米煎饼班: 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; ...

我有一个卡萨迪玉米煎饼班:

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;
...
}

你需要一个
所有CasaDeBurrito收藏夹的集合,按朋友的名字排序
,所以我想说一个
地图
就是你需要的,每个键都有一个朋友的名字,值是他喜欢使用你的
收藏夹Byrating
方法的
列表,全部按名字排序(使用
树形图


因此,我不知道如何正确提问,但我设法将评论和答案与我需要的联系起来。 我按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>
返回p.getFriends().stream()//stream
.sorted(Comparator.comparingit(Profesor::getId))//流
.map(P->P.FavoritesByList(0))//流
.flatMap(集合::流)//流
.distinct()
.collect(Collectors.toList())//列表

我将再次编辑我的问题,使其更加清楚

您想要什么作为输出?似乎您想要
flatMap
…您可能正在寻找flatMap:您能告诉我们您的模型的缺点吗,就像这样,我们无法理解您想要实现的
forEach
是一个
void
终端操作。它不会按要求返回结果。请注意flatMap功能,下面是一个很好的示例,我可以将map的值列表展平吗?我的意思是列出所有值的列表,然后按照它们的建议将其展平above@GuySadoun这是第二个代码所做的,但这与:p.getFriends().stream().map(prof->prof.favoritesByRating(0)).flatMap(List::stream)相同
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;
}
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));
}
public List<CasaDeBurrito> favoritesByRating(Profesor p) {
    return p.getFriends().stream()
            .flatMap(prof -> prof.favoritesByRating(0).stream())
            .collect(toList());
}
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>