Chaining a dedicated terminal stream operation might be considered more expressive by those being used to chained method calls rather than the “LISP style” of composed collector factory calls. But it also allows optimized execution strategies for the stream implementation, as it knows the actual operation instead of just seeing a Collector
abstraction.
On the other hand, as you named it yourself, Collector
s can be composed, allowing to perform these operation embedded in another collector, at places where stream operations are not possible anymore. I suppose, this mirroring become apparent only at a late stage of the Java?8 development, which is the reason why some operations lacked their counterpart, like filtering
or flatMapping
, which will be there only in Java?9. So, having two different APIs doing similar things, was not a design decision made at the start of the development.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…