Java API中的groupingBy是什么意思,如何使用?
What does it mean in groupingBy in the Java API and how to use it?
我正在查看关于收藏家的 Java API。
public static <T,K,D,A,M extends Map<K,D>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T,A,D> downstream)
Type Parameters:
T - the type of the input elements
K - the type of the keys
A - the intermediate accumulation type of the downstream collector
D - the result type of the downstream reduction
M - the type of the resulting Map
Parameters:
classifier - a classifier function mapping input elements to keys
downstream - a Collector implementing the downstream reduction
mapFactory - a function which, when called, produces a new empty Map
of the desired type
Returns:
a Collector implementing the cascaded group-by operation
从第一行代码,我明白这是一个public静态方法,但是<T, K, D, A, M extends Map<K, D>>
是什么意思?
然后从Collector<T, ?, M>
可以理解为returns一个Collector但是?
.
是什么意思
此外,我进入了其中一个参数 Supplier
的页面,因为它是一个函数式接口,所以它只有一个名为 get
的抽象方法,但我根本不知道如何使用它根本。我正在阅读的这本书教我们使用 TreeMap::new
.
我的困惑是为什么参数需要实现 Supplier
接口的东西,但我们却能够使用方法引用来代替?
提前致谢!
<T,K,D,A,M extends Map<K,D>>
- 这些是此函数的通用参数,用作此函数的参数和结果通用参数。所以,你的参数必须是:
Function<? super T,? extends K> classifier
- 采用 T
和 returns K
的函数
Supplier<M> mapFactory
- 供应商创建 M
Collector<? super T,A,D> downstream
- 带有 T
、A
和 D
参数的收集器(有关详细信息,请查看进一步的解释和文档)
而且此函数还生成 Collector<T,?,M>
- 这里是 T
和 M
。所有这些参数都应显式或隐式指定。
关于收集器参数,你可以去Collector documentation page看看,Collector
class的第二个通用参数是
the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
通常,你不会关心这个可变的累加类型,因为最后你只需要结果,在这个例子中,就是M
.
关于供应商 - 我也建议去 documentation page。它是一个只有一种方法的接口 T get()
。而这个TreeMap::new
可以表示为() -> new TreeMap<>()
。如果这也不清楚,那么您可能需要阅读一些关于匿名 classes、功能接口和 lambda 函数(以及方法引用)的教程。
<T, K, D, A, M extends Map<K, D>>
表示此方法使用 5 个泛型类型参数 - T
、K
、D
、A
和 M
其中 M
必须实现 Map<K, D>
.
?
表示通配符,即您不关心它是什么类型。请注意,这与添加另一个泛型类型参数不同。如果它是另一个泛型类型参数,比如 U
,它可以在代码的后面引用。你关心。
Supplier
表示不接受参数的方法,returns 表示某种类型的值。 TreeMap::new
不带参数,给你一个TreeMap
,所以这里可以参考这个方法
我正在查看关于收藏家的 Java API。
public static <T,K,D,A,M extends Map<K,D>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T,A,D> downstream)
Type Parameters:
T - the type of the input elements
K - the type of the keys
A - the intermediate accumulation type of the downstream collector
D - the result type of the downstream reduction
M - the type of the resulting Map
Parameters:
classifier - a classifier function mapping input elements to keys
downstream - a Collector implementing the downstream reduction
mapFactory - a function which, when called, produces a new empty Map of the desired type
Returns:
a Collector implementing the cascaded group-by operation
从第一行代码,我明白这是一个public静态方法,但是<T, K, D, A, M extends Map<K, D>>
是什么意思?
然后从Collector<T, ?, M>
可以理解为returns一个Collector但是?
.
此外,我进入了其中一个参数 Supplier
的页面,因为它是一个函数式接口,所以它只有一个名为 get
的抽象方法,但我根本不知道如何使用它根本。我正在阅读的这本书教我们使用 TreeMap::new
.
我的困惑是为什么参数需要实现 Supplier
接口的东西,但我们却能够使用方法引用来代替?
提前致谢!
<T,K,D,A,M extends Map<K,D>>
- 这些是此函数的通用参数,用作此函数的参数和结果通用参数。所以,你的参数必须是:
Function<? super T,? extends K> classifier
- 采用T
和 returnsK
的函数
Supplier<M> mapFactory
- 供应商创建M
Collector<? super T,A,D> downstream
- 带有T
、A
和D
参数的收集器(有关详细信息,请查看进一步的解释和文档)
而且此函数还生成 Collector<T,?,M>
- 这里是 T
和 M
。所有这些参数都应显式或隐式指定。
关于收集器参数,你可以去Collector documentation page看看,Collector
class的第二个通用参数是
the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
通常,你不会关心这个可变的累加类型,因为最后你只需要结果,在这个例子中,就是M
.
关于供应商 - 我也建议去 documentation page。它是一个只有一种方法的接口 T get()
。而这个TreeMap::new
可以表示为() -> new TreeMap<>()
。如果这也不清楚,那么您可能需要阅读一些关于匿名 classes、功能接口和 lambda 函数(以及方法引用)的教程。
<T, K, D, A, M extends Map<K, D>>
表示此方法使用 5 个泛型类型参数 -T
、K
、D
、A
和M
其中M
必须实现Map<K, D>
.?
表示通配符,即您不关心它是什么类型。请注意,这与添加另一个泛型类型参数不同。如果它是另一个泛型类型参数,比如U
,它可以在代码的后面引用。你关心。Supplier
表示不接受参数的方法,returns 表示某种类型的值。TreeMap::new
不带参数,给你一个TreeMap
,所以这里可以参考这个方法