小编Mau*_*ida的帖子

可以在不使用终端操作的情况下知道流的大小

我有3个接口

public interface IGhOrg {
    int getId();

    String getLogin();

    String getName();

    String getLocation();

    Stream<IGhRepo> getRepos();
}

public interface IGhRepo {
    int getId();

    int getSize();

    int getWatchersCount();

    String getLanguage();

    Stream<IGhUser> getContributors();
}

public interface IGhUser {
    int getId();

    String getLogin();

    String getName();

    String getCompany();

    Stream<IGhOrg> getOrgs();
}
Run Code Online (Sandbox Code Playgroud)

我需要实施 Optional<IGhRepo> highestContributors(Stream<IGhOrg> organizations)

此方法返回一个包含大多数贡献者的 IGhRepo(getContributors())

我试过这个

Optional<IGhRepo> highestContributors(Stream<IGhOrg> organizations){
    return organizations
            .flatMap(IGhOrg::getRepos)
            .max((repo1,repo2)-> (int)repo1.getContributors().count() - (int)repo2.getContributors().count() );
}
Run Code Online (Sandbox Code Playgroud)

但它给了我

java.lang.IllegalStateException: 流已经被操作或关闭

我知道count()是Stream中的一个终端操作但是我无法解决这个问题,请帮忙!

谢谢

java-8 java-stream

4
推荐指数
2
解决办法
5837
查看次数

标签 统计

java-8 ×1

java-stream ×1