简体   繁体   English

Java:汇总操作

[英]Java: Aggregate Operations

I'm studying generics, functional interfaces, lambda expressions, and aggregate operations. 我正在研究泛型,功能接口,lambda表达式和聚合操作。 Although I understand these technologies individually, I'm having trouble conceptualizing them collectively, ie, how they work together. 尽管我分别理解这些技术,但是在将它们概念化(即它们如何协同工作)时遇到了麻烦。

Using an example taken from the Java Doc titled: interface Collector(T,A,R) : 使用摘自Java Doc的示例,示例: interface Collector(T,A,R)

Collector(Widget,?,TreeSet(Widget)) into = Collector.of(TreeSet::new, TreeSet::add, (left,right)->{left.addAll(right); return left;});

The above method, in the aforementioned javadoc, has the following behavioral parameters: 上述Javadoc中的上述方法具有以下行为参数:

Supplier(A) supplier,
BiConsumer(A,T) accumulator,
BiConsumer(A) combiner

The supplier provided in the above example uses the method reference: TreeSet::new . 上例中提供的供应商使用方法参考: TreeSet::new

What is the relationship of the TreeSet::new , being used as the supplier of results, to the accumulator?..to that of the combiner? 被用作结果提供者的TreeSet::new与累加器之间是什么关系?与组合器之间是什么关系?

How does this work, I don't understand how this is a supplier!? 如何运作,我不知道这是供应商!

Any documentation links concerning this broad subject matter would be worth much gold and silver. 任何涉及这一广泛主题的文献链接都将价值不菲。 Thanks for your patience. 谢谢你的耐心。

Quotes directly from the Java documentation : 直接引用Java文档

The Supplier: 供应商:

A function that creates and returns a new mutable result container. 创建并返回新的可变结果容器的函数。

The TreeSet::new is just a call to the constructor of TreeSet, which returns an instance of TreeSet. TreeSet::new只是对TreeSet的构造函数的调用,该构造函数返回TreeSet的实例。 In this sense it is a supplier of TreeSets. 从这个意义上讲,它是TreeSet的供应商。

The BiConsumer, called the accumulator in this context, represents BiConsumer在此上下文中称为累加器,它表示

A function that folds a value into a mutable result container. 将值折叠到可变结果容器中的函数。

It will offer objects of type T to the object of type A. Typically you will just call some add method of a collection, but you can also do some pre-processing here when defining a custom collector. 它将提供类型T的对象到类型A的对象。通常,您将只调用集合的某些add方法,但是在定义自定义收集器时,也可以在此处进行一些预处理。

Lastly the BiConsumer is described as: 最后,BiConsumer描述为:

A function that accepts two partial results and merges them. 接受两个部分结果并将其合并的函数。

It is used when a process is executed in parallel and partial results (in this context, TreeSet objects with only a subset of the elements) need to be combined into the final TreeSet result. 当进程并行执行并且部分结果(在这种情况下,仅包含元素子集的TreeSet对象)需要组合到最终的TreeSet结果中时,将使用该方法。 This would typically be an addAll method. 这通常是一个addAll方法。

So by providing these necessary ingredients, you are able to construct your own collection mechanisms. 因此,通过提供这些必要的成分,您可以构建自己的收集机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM