Having Stream<Stream> how can we get the Stream that contains more elements in the other Stream

Asked

Viewed 116 times

0

I have 3 interfaces

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();
}
  • I want to get Ighrepo with more users to contribute
    (Stream<IGhUser> getContributors())

Method signature:

Optional<IGhRepo> highestContributors(Stream<IGhOrg> organizations)
  • What I’ve done to achieve the goal, but without success:

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

The result is this exception:

java.lang.Illegalstateexception: stream has already been operated upon or closed

NOTE: I understand that the count() is a terminal operation of the class Stream and that’s why you give me this exception.

I can’t solve this problem, please help.

Thank you.

  • 1

    This problem depends a lot on what the implementation of your methods that return the Streams do. In particular, if the getContributors() is creating a Stream and returning it (which is probably correct) or if it is always returning the same one Stream although this has already been used (which is probably wrong). Therefore, to answer your question, you will need to show at least a few snippets of the implementations of these interfaces.

  • Exactly. It’s hard to tell what’s going on if you don’t put the implementation here. But it’s more possible that you’re reusing Stream somehow.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.