Reputation: 365
I want to create my own stateful operation with streams, for example, I want to create a custom reduce operation, that combine some of the items and some not (according to my rules), for example: [1, 2, 3, 4] -> [1 + 2, 3 + 4].
It will be nice if I could implement my version of java.util.stream.DistinctOps
but since I can't, I need another way to implement my operations.
Meanwhile, in order to implement this operation, I collected the stream, performed my operation and then returned a new stream:
public Stream<Integer> myOperation(Stream<Integer> stream) {
List<Integer> list = stream.collect(toList());
list = preform operation...
return list.stream();
}
But I want to implement this operation without performing a terminal operation, Any ideas?.
Thanks.
Upvotes: 2
Views: 247
Reputation: 198211
But I want to implement this operation without performing a terminal operation, Any ideas?.
There is really, truly, no way to do it without performing a terminal operation. All intermediate, nonterminal operations on streams are "rigid" in the sense that they don't permit you to perform custom behavior like this.
Upvotes: 2