为什么Java Stream API中的某些收集器被称为下游收集器?

huangapple go评论103阅读模式
英文:

Why is certain Collectors in Java stream API called downstream collector?

问题

我想知道为什么我们会将某些收集器分类为“下游收集器”?那么是否有上游收集器呢?请注意,这不是关于用法,而是试图理解“下游”一词背后的逻辑。对我来说,当您通常处理流 API 用法时,所有沿着构建器链向下的流似乎都只是下游。

List<String> list = List.of("AAA","B","CCCCC","DDD", "FFFFFF", "AAA");
List<Integer> res =
            list.stream()
                    .collect(
                            Collectors.mapping(s -> s.length(), // string -> int
                                Collectors.toList())); // 下游操作

在上面的代码中,`Collectors.toList()` 被视为下游操作
英文:

I wanted to know why we classify certain collectors as "downstream"? Is there an upstream Collector then? Please note that this is not about usage, but trying to understand the logic behind the term "downstream". To me, when you normally deal with stream API usage, all streams down the builder chain looks like they are downstream only.

List&lt;String&gt; list = List.of(&quot;AAA&quot;,&quot;B&quot;,&quot;CCCCC&quot;,&quot;DDD&quot;, &quot;FFFFFF&quot;, &quot;AAA&quot;);
List&lt;Integer&gt; res =
            list.stream()
                    .collect(
                            Collectors.mapping(s -&gt; s.length(), // string -&gt; int
                                Collectors.toList())); // downstreaming

In the above code, Collectors.toList() is regarded as downstream.

答案1

得分: 10

术语“downstream”在文档中指的是一个收集器(Collector)将另一个收集器作为参数接受的情况。该参数在接受它的收集器之后被应用。换句话说,下游收集器会应用于上游收集器的结果。

在你的示例中,Collectors.toList 是从 Collectors.mapping 下游的。

英文:

The term downstream in the documentation refers to one Collector accepting a second Collector as an argument. The argument is applied downstream (after) the Collector that accepts it. In other words, the downstream Collector is applied to the result of the upstream Collector.

In your example, Collectors.toList is downstream from Collectors.mapping.

答案2

得分: 8

我经常把流 API 想象成一个产品的生产线。原材料从某处获得(ArrayList.streamIntStream.rangeStream.of,或其他什么),放在传送带上,然后通过中间方法对材料进行转换(map/flatMap 等)和过滤(filter/limit 等),最后它们到达生产线的末端,组装成一个最终产品(collect*

Collector 是“机器”,用于构建前面提到的不同最终产品。toList 构建一个列表,toSet 构建一个集合,等等。然而,其他的收集器并没有完全构建这个大物体,例如 groupingBygroupingBy 仅按键对材料进行分组,然后再次将项目作为组排放回传送带。这些收集器需要另一个收集器在生产线下游(也就是下游)继续构建最终产品。

mapping 是另一个不完全构建最终产品的收集器。它只是简单地转换材料,然后再次排放出来,有点像 map。它的用途在于当您想要转换从 groupingBy 中排放出来的组时。也就是说,当您将其用作另一个收集器的下游时,它主要是有用的。

> 那么是否有上游收集器呢?

根据生产线的类比,这种关系是双向的:toListmapping 的下游,所以 mappingtoList 的上游。但在官方文档中,这个词并没有被经常提到。我只在 peek 中找到过。

*还有其他终端操作,但让我们专注于 collect,因为问题是关于它的。

英文:

I often imagine the stream API as building a production line of a product. There are raw materials coming from somewhere (ArrayList.stream, IntStream.range, Stream.of, whatever), on a conveyer belt, and then with intermediate methods, the materials get transformed (map/flatMap etc) and filtered (filter/limit etc) and finally they reach the end of the line, where they get assembled into one final product (collect)<sup>*</sup>.

Collectors are "machines" that build different final products aforementioned. toList builds a list. toSet builds a Set etc. However, other collectors doesn't fully build the big thing, e.g. groupingBy. groupingBy only groups the materials by a key, and then spits the items out again, as groups, back on the conveyor belt. These collectors need another collector down the production line (aka down the stream) to continue building the final product.

mapping is another one of those collectors that doesn't completely build the final product. It merely transforms the materials and spits them out again, which is kind of like map. It's usefulness comes when you want to, e.g. transform the groups spitted out from a groupingBy. i.e. It's mostly useful when you use it as the downstream of another collector.

> Is there an upstream Collector then?

Following the production line analogy, the relationship is two way: toList is the downstream of mapping, so mapping is the upstream of toList. In official documentation though. This word isn't mentioned much. I only found it in peek.

<sup>*</sup>There are other terminal operations, but let's focus on collect, since this is what the question is about.

huangapple
  • 本文由 发表于 2020年8月15日 09:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63421760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定