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.
This problem depends a lot on what the implementation of your methods that return the
Stream
s do. In particular, if thegetContributors()
is creating aStream
and returning it (which is probably correct) or if it is always returning the same oneStream
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.– Victor Stafusa
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.
– rdllopes