Multimap
允许一个key映射多个值
通常比较少直接使用Multimap,而是使用ListMultimap或SetMultimap
常用API
Signature | Description | Equivalent |
put(K, V) | Adds an association from the key to the value | multimap.get(key).add(value) |
putAll(K, Iterable<V>) | Adds associations from the key to each of the values in turn | Iterables.addAll(multimap.get(key), values) |
remove(K, V) | Removes one association from key to value and returns true if the multimap changed. | multimap.get(key).remove(value) |
removeAll(K) | Removes and returns all the values associated with the specified key. The returned collection may or may not be modifiable, but modifying it will not affect the multimap. (Returns the appropriate collection type.) | multimap.get(key).clear() |
replaceValues(K, Iterable<V>) | Clears all the values associated with key and sets key to be associated with each of values. Returns the values that were previously associated with the key. | multimap.get(key).clear(); Iterables.addAll(multimap.get(key), values) |
实现类
Implementation | Keys behave like... | Values behave like.. |
ArrayListMultimap | HashMap | ArrayList |
HashMultimap | HashMap | HashSet |
LinkedListMultimap * | LinkedHashMap``* | LinkedList``* |
LinkedHashMultimap** | LinkedHashMap | LinkedHashSet |
TreeMultimap | TreeMap | TreeSet |
ImmutableListMultimap | ImmutableMap | ImmutableList |
ImmutableSetMultimap | ImmutableMap | ImmutableSet |