Guava 的 Multimaps class 的 index() 函数在内部是如何工作的?
How Guava's Multimaps class's index() function works internally?
我正在尝试了解 Multimaps class's index 函数。如果我想像 HashMap 一样使用它,那么我可以将多个值放在一个键上并使用相同的键检索它们。
但是如果我想根据某些标准对数据进行分组,那么会使用像 this 这样的 Multimaps 实现。
现在我对下面的索引函数声明有疑问。
public static <K,V> ImmutableListMultimap<K,V> index(Iterator<V> values, Function<? super V,K> keyFunction)
如果要用<K,V>
返回ImmutableListMultimap,那为什么Function有<? super V,K>
的类型声明,正好相反?
另外,函数类型的匿名内部 class 如何工作,如 this 示例所示?我无法理解谁调用了匿名内部 class 函数中定义的 apply 方法?
If ImmutableListMultimap is to be returned with <K,V> then why does Function have the type declaration of <? super V,K>, which is exactly opposite?
Multimap 有两个类型参数 K
作为键,V
作为值。 index
方法具有参数 Iterator<V> values
(显然用于值)和 Function<? super V,K> keyFunction
(用于为值生成键)。
这意味着 keyFunction
必须接受一个值(类型 V
或其超类型之一,因为您可以将类型 V
的任何值传递给接受的方法V
) 的超类型,并且它必须 return 该值的键(K
类型)。这导致类型 Function<? super V,K>
.
Also how does the anonymous inner class of type Function works as shown in this example? I am not able to understand who calls the apply method defined inside the anonymous inner class Function?
如果查看 index
方法 (https://github.com/google/guava/blob/v23.0/guava/src/com/google/common/collect/Multimaps.java#L1630) 的实现,您会看到 index
方法调用 keyFunction.apply(value)
[=27 的第 1637 行=]
我正在尝试了解 Multimaps class's index 函数。如果我想像 HashMap 一样使用它,那么我可以将多个值放在一个键上并使用相同的键检索它们。
但是如果我想根据某些标准对数据进行分组,那么会使用像 this 这样的 Multimaps 实现。
现在我对下面的索引函数声明有疑问。
public static <K,V> ImmutableListMultimap<K,V> index(Iterator<V> values, Function<? super V,K> keyFunction)
如果要用<K,V>
返回ImmutableListMultimap,那为什么Function有<? super V,K>
的类型声明,正好相反?
另外,函数类型的匿名内部 class 如何工作,如 this 示例所示?我无法理解谁调用了匿名内部 class 函数中定义的 apply 方法?
If ImmutableListMultimap is to be returned with <K,V> then why does Function have the type declaration of <? super V,K>, which is exactly opposite?
Multimap 有两个类型参数 K
作为键,V
作为值。 index
方法具有参数 Iterator<V> values
(显然用于值)和 Function<? super V,K> keyFunction
(用于为值生成键)。
这意味着 keyFunction
必须接受一个值(类型 V
或其超类型之一,因为您可以将类型 V
的任何值传递给接受的方法V
) 的超类型,并且它必须 return 该值的键(K
类型)。这导致类型 Function<? super V,K>
.
Also how does the anonymous inner class of type Function works as shown in this example? I am not able to understand who calls the apply method defined inside the anonymous inner class Function?
如果查看 index
方法 (https://github.com/google/guava/blob/v23.0/guava/src/com/google/common/collect/Multimaps.java#L1630) 的实现,您会看到 index
方法调用 keyFunction.apply(value)
[=27 的第 1637 行=]