为什么guava Multimap.values()返回一个平面集合而不是集合集合?

时间:2021-07-22 15:51:47

I really like the Multimap class of the google guava library. It is a map type where you can add multiple values for a key, so it effectively maps from a key to a collection of some type. What I especially love is the Multimaps.index() function which takes an Iterable and a key function and returns a Multimap which groups (or indexes or maps) the elements of the Iterable by the value the function returns for each of those elements.

我非常喜欢谷歌番石榴图书馆的多地图类。它是可以为键添加多个值的映射类型,因此它可以有效地将键映射到某个类型的集合。我特别喜欢的是Multimap .index()函数,它使用一个可迭代的和一个键函数,并返回一个多映射,该多映射组(或索引或映射)的元素可迭代,其值为每个元素的函数返回值。

What I find a bit strange is that Multimap.values() returns a flat collection instead of a collection of collections? So the grouping the index function gave me is lost once Ì retrieve the values. I can circumvent that problem by calling Multimap.asMap() and then call values() on that.

我觉得有点奇怪的是,Multimap.values()返回一个平面集合而不是集合集合?因此,一旦我检索到这些值,索引函数给我的分组就会丢失。我可以通过调用Multimap.asMap()并在其上调用values()来绕过这个问题。

Does anyone know why it may make sense that Multimap behaves that way?

有人知道为什么Multimap会这样做吗?

2 个解决方案

#1


13  

Multimap.asMap().values() isn't a way around the problem -- it was deliberate that Multimap provides both ways of accessing it, getting a Collection<Collection<V>> via asMap().values() and getting the flattened Collection<V> with values().

Multimap.asMap().values()并不是解决问题的方法——人们认为Multimap提供了访问它的两种方式,通过asMap().values()获取集合 >,以及使用value()获取扁平集合

More generally speaking, Multimap tries not to just be "a map to collections," but rather "a general way to associate keys with multiple values." So you get the entries() method in addition to values() and keys(). The asMap() view provides a way to treat it as a "map to collections," but that has very different semantics that aren't always what you're looking for.

更一般地说,Multimap不只是“集合的映射”,而是“将键与多个值关联的通用方式”。因此,除了values()和keys()之外,还可以获得entries()方法。asMap()视图提供了一种将其视为“集合映射”的方法,但它的语义非常不同,并不总是您所要寻找的。

In any event, the values method is just meant to fill a different niche than the one filled by asMap().values().

在任何情况下,值方法都只是为了填充一个不同的细分市场,而不是由asMap().values()填充的。

#2


6  

Does anyone know why it may make sense that Multimap behaves that way?

有人知道为什么Multimap会这样做吗?

A Multimap should be viewed as ordinary map, where the keys does not need to be unique.

多映射应该被视为普通映射,其中的键不需要是唯一的。

Key       Val
 a   ->    1
 b   ->    2
 a   ->    3

Values: {1, 2, 3}

#1


13  

Multimap.asMap().values() isn't a way around the problem -- it was deliberate that Multimap provides both ways of accessing it, getting a Collection<Collection<V>> via asMap().values() and getting the flattened Collection<V> with values().

Multimap.asMap().values()并不是解决问题的方法——人们认为Multimap提供了访问它的两种方式,通过asMap().values()获取集合 >,以及使用value()获取扁平集合

More generally speaking, Multimap tries not to just be "a map to collections," but rather "a general way to associate keys with multiple values." So you get the entries() method in addition to values() and keys(). The asMap() view provides a way to treat it as a "map to collections," but that has very different semantics that aren't always what you're looking for.

更一般地说,Multimap不只是“集合的映射”,而是“将键与多个值关联的通用方式”。因此,除了values()和keys()之外,还可以获得entries()方法。asMap()视图提供了一种将其视为“集合映射”的方法,但它的语义非常不同,并不总是您所要寻找的。

In any event, the values method is just meant to fill a different niche than the one filled by asMap().values().

在任何情况下,值方法都只是为了填充一个不同的细分市场,而不是由asMap().values()填充的。

#2


6  

Does anyone know why it may make sense that Multimap behaves that way?

有人知道为什么Multimap会这样做吗?

A Multimap should be viewed as ordinary map, where the keys does not need to be unique.

多映射应该被视为普通映射,其中的键不需要是唯一的。

Key       Val
 a   ->    1
 b   ->    2
 a   ->    3

Values: {1, 2, 3}