Java – why does the stream operation repeat with the collector?

Allow me to make some complaints, which may be interesting, but I want to describe: "why did you raise this question?"

After my in-depth study, I found that there are many repetitive logics between stream and collector that violate the don't repeat yourself principle, such as stream #map & collectors #mapping, stream #filter & collectors #filtering in jdk-9 and etc.

However, it seems reasonable to follow the tell, don't ask principle since stream / Law of Demeter and collector follow the composition over inheritance principle

I can only think of the following reasons why the stream operation is repeated with the collector:

>We don't care how to create a stream in a large environment In this case, the stream operation is more efficient and faster than the collector because it can simply map a stream to another stream, for example:

consuming(stream.map(...));
consuming(stream.collect(mapping(...,toList())).stream());

void consuming(Stream<?> stream){...}

>Collectors are more powerful and can be combined to collect elements in a stream. However, stream only provides some useful / highly used operations For example:

stream.collect(groupingBy(
  ...,mapping(
        ...,collectingAndThen(reducing(...),...)
       )
));

>Stream operations are more expressive than collectors, but they are slower than collectors because it will create a new stream for each operation, and stream is heavier and more abstract than collectors, for example:

stream.map(...).collect(collector);
stream.collect(mapping(...,collector));

>The collector cannot apply a short-circuit terminal operation as a stream For example:

stream.filter(...).findFirst();

Can anyone suggest other disadvantages / advantages and why does the stream operation duplicate the collector? I want to understand them again Thank you in advance

Solution

Link specific terminal flow operations may be considered more expressive of the "LISP style" used to link method calls rather than composite collector factory calls But it also allows the optimized execution strategy of flow implementation because it knows the actual operation rather than just seeing the collector abstraction

On the other hand, as you name it, you can combine collectors to allow these operations embedded in another collector to be performed where streaming operations are no longer possible I think this kind of image becomes obvious only in the later stage of Java 8 development, which is why some operations lack corresponding operations, such as filter or flatmapping, which only exist in Java 9 Therefore, there are two different APIs doing similar things, not design decisions made at the beginning of development

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>