为什么 Multimap 不是驼峰式的?
Why is Multimap not camel cased?
这个真的让我(和我的同事)很烦。
不是
- 哈希图
- 树状图
- org.apache.commons.collections.Multimap
- 等等
那么为什么没有人注意到这个命名约定缺陷或者这个错字背后有什么意图?
这不是错字。 Guava 的 Multimap
是 而不是 一个 Map
,即它不扩展 Map
接口(也不应该)。见 Guava's wiki page on this topic:
Multimap.get(key)
always returns a non-null, possibly empty collection. This doesn't imply that the multimap spends any memory associated with the key, but instead, the returned collection is a view that allows you to add associations with the key if you like.
- If you prefer the more
Map
-like behavior of returning null
for keys that aren't in the multimap, use the asMap()
view to get a Map<K, Collection<V>>
. (Or, to get a Map<K,
List
<V>>
from a ListMultimap
, use the static Multimaps.asMap()
method. Similar methods exist for SetMultimap
and SortedSetMultimap
.)
Multimap.containsKey(key)
is true if and only if there are any elements associated with the specified key. In particular, if a key k
was previously associated with one or more values which have since been removed from the multimap, Multimap.containsKey(k)
will return false.
Multimap.entries()
returns all entries for all keys in the Multimap
. If you want all key-collection entries, use asMap().entrySet()
.
Multimap.size()
returns the number of entries in the entire multimap, not the number of distinct keys. Use Multimap.keySet().size()
instead to get the number of distinct keys.
另一方面,Apache Commons Collections' MultiMap
(not the capital "M" in map) extends Map
, but it's a bit awkward in use, plus Apache devs also came to consludion that extending and mimicking map-like behavior in multimap is not what user wants, so they deprecated MultiMap
(yes, you should not use old MultiMap
interface and its implementations in new code!) and now recommend using MultiValueMap
- 它 不是 扩展 Map
并且 API 与 Guava 相当。
单词“multimap”(一个单词,完全小写)是指特定的数据结构。它不同于"map",后者是另一种数据结构。因此,由于它们是不同的数据结构,因此它们具有不同的名称。
Map
interface you usually use in Java is the chosen name for an associative array, also known as "map", "dictionary", "hash", etc. Likewise, Guava's Multimap
接口是它们对 multimap
数据结构的表示。
这个真的让我(和我的同事)很烦。
不是
- 哈希图
- 树状图
- org.apache.commons.collections.Multimap
- 等等
那么为什么没有人注意到这个命名约定缺陷或者这个错字背后有什么意图?
这不是错字。 Guava 的 Multimap
是 而不是 一个 Map
,即它不扩展 Map
接口(也不应该)。见 Guava's wiki page on this topic:
Multimap.get(key)
always returns a non-null, possibly empty collection. This doesn't imply that the multimap spends any memory associated with the key, but instead, the returned collection is a view that allows you to add associations with the key if you like.- If you prefer the more
Map
-like behavior of returningnull
for keys that aren't in the multimap, use theasMap()
view to get aMap<K, Collection<V>>
. (Or, to get aMap<K,
List
<V>>
from aListMultimap
, use the staticMultimaps.asMap()
method. Similar methods exist forSetMultimap
andSortedSetMultimap
.)Multimap.containsKey(key)
is true if and only if there are any elements associated with the specified key. In particular, if a keyk
was previously associated with one or more values which have since been removed from the multimap,Multimap.containsKey(k)
will return false.Multimap.entries()
returns all entries for all keys in theMultimap
. If you want all key-collection entries, useasMap().entrySet()
.Multimap.size()
returns the number of entries in the entire multimap, not the number of distinct keys. UseMultimap.keySet().size()
instead to get the number of distinct keys.
另一方面,Apache Commons Collections' MultiMap
(not the capital "M" in map) extends Map
, but it's a bit awkward in use, plus Apache devs also came to consludion that extending and mimicking map-like behavior in multimap is not what user wants, so they deprecated MultiMap
(yes, you should not use old MultiMap
interface and its implementations in new code!) and now recommend using MultiValueMap
- 它 不是 扩展 Map
并且 API 与 Guava 相当。
单词“multimap”(一个单词,完全小写)是指特定的数据结构。它不同于"map",后者是另一种数据结构。因此,由于它们是不同的数据结构,因此它们具有不同的名称。
Map
interface you usually use in Java is the chosen name for an associative array, also known as "map", "dictionary", "hash", etc. Likewise, Guava's Multimap
接口是它们对 multimap
数据结构的表示。