I have a stream, where each object is identified by a unique id.
Moreover, each object has a either positive or negative Free
values.
I want to divide this stream into two Sets, where one contains the ids
whose Free
values was positive, and one with the rest.
But I find the following not being the right way, as I'm collecting into lists outside of the stream.
class Foo {
int free;
long id;
}
public Tuple2<Set<Long>, Set<Long>> findPositiveAndNegativeIds() {
Set<Long> positives = new HashSet<>();
Set<Long> negatives = new HashSet<>();
foos.stream()
.forEach(f -> {
if (f.free >= 0) positigves.add(f.id);
else negatives.add(f.id);
});
return Tuple2.tuple(positives, negatives);
}
Could this be done better, somehow with partitionBy()
or similar?