So, the question is pretty self-explanatory: is there a way to create a Collector, that is collecting stream that is passed to it into a new stream?
I am aware that it can be done with some tricks like:
Collectors.collectingAndThen(Collectors.toList(), List::stream);
But this code allocates redundant memory.
Explanation on why do I need this in the first place: sometimes I want to pass something to Collectors.groupingBy
and then perform a stream operation on a downstream, without collecting it additional time.
Is there a simple way to do it (without writing my own class implementing the collector interface)?
EDIT: This question has been marked as a duplicate of this question, but that is not what I'm looking for. I do not want to duplicate the original stream, I want to close it and produce a new stream consisting of the same elements in the same order, by the means of Collector and without allocating memory in between.