Interface Multimap<K,V>
- Type Parameters:
K
- the type of keys maintained by this multimapV
- the type of values that can be associated with the keys
- All Superinterfaces:
ImmutableMultimap<K,
V>
- All Known Implementing Classes:
ArtifactMultimap
,LinkedSetMultimap
,ListMultimap
,SetMultimap
,TreeMultimap
The
Multimap
interface defines a collection that maps keys to values, similar to Map
, but in which
each key may be associated with multiple values. Implementations of this interface manage collections of keys and
values where the values are typically stored in a collection under a single key.
This interface provides a unified approach to handling multiple values associated with a single key which is a common requirement in many applications. The implementations can vary in the type of collections they use to store the values associated with the keys, such as lists, sets, etc.
- Since:
- 10.0
-
Method Summary
Modifier and TypeMethodDescriptionMap<K,
Collection<V>> asMap()
Returns a view of the mappings contained in this multimap as a map from each key to its corresponding values.boolean
containsKey
(K key) Returnstrue
if this multimap contains one or more keys to the specified value.copy()
Creates a deep copy of this multimap.entries()
Returns a collection view of all key-value pairs contained in this multimap.Retrieves the collection of values associated with the specified key in this multimap.inverse()
Creates an inverse view of this multimap.boolean
isEmpty()
Returnstrue
if this multimap contains no key-value mappings.keySet()
Returns the set of keys contained in this multimap.void
Associates the specified value with the specified key in this multimap.void
putAll
(K key, Collection<? extends V> values) Associates all of the values in the specified collection with the specified key in this multimap.void
Removes a single instance of the specified value from the multimap, if it is present.void
Removes all the values associated with the specified key in this multimap.int
size()
Returns the total number of key-value pairs in the multimap.values()
Returns a collection view of all values associated with keys in this multimap.
-
Method Details
-
put
Associates the specified value with the specified key in this multimap. If the multimap previously contained a mapping for the key, the new value is added to the collection of values mapped to that key.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key
-
putAll
Associates all of the values in the specified collection with the specified key in this multimap. If the multimap previously contained mappings for the key, the new values are added to the collection of values mapped to that key.- Parameters:
key
- key with which the specified values are to be associatedvalues
- collection of values to be associated with the specified key
-
get
Retrieves the collection of values associated with the specified key in this multimap.- Specified by:
get
in interfaceImmutableMultimap<K,
V> - Parameters:
key
- the key whose associated values are to be returned- Returns:
- the collection of values associated with the specified key, or an empty collection if no values are associated with the key
-
remove
Removes a single instance of the specified value from the multimap, if it is present.- Parameters:
key
- key with which the specified value is associatedvalue
- value to be removed from the collection of values for the specified key
-
removeAll
Removes all the values associated with the specified key in this multimap.- Parameters:
key
- key whose associated values are to be removed
-
keySet
Returns the set of keys contained in this multimap.- Specified by:
keySet
in interfaceImmutableMultimap<K,
V> - Returns:
- the set of keys contained in this multimap
-
containsKey
Returnstrue
if this multimap contains one or more keys to the specified value.- Specified by:
containsKey
in interfaceImmutableMultimap<K,
V> - Parameters:
key
- key whose presence in this multimap is to be tested- Returns:
true
if this multimap contains a mapping for the specified key
-
values
Collection<V> values()Returns a collection view of all values associated with keys in this multimap.- Specified by:
values
in interfaceImmutableMultimap<K,
V> - Returns:
- a collection view of all values contained in this multimap
-
isEmpty
boolean isEmpty()Returnstrue
if this multimap contains no key-value mappings.- Specified by:
isEmpty
in interfaceImmutableMultimap<K,
V> - Returns:
true
if this multimap contains no key-value mappings
-
asMap
Map<K,Collection<V>> asMap()Returns a view of the mappings contained in this multimap as a map from each key to its corresponding values.- Specified by:
asMap
in interfaceImmutableMultimap<K,
V> - Returns:
- a map view of the mappings contained in this multimap
-
entries
Collection<Map.Entry<K,V>> entries()Returns a collection view of all key-value pairs contained in this multimap. Each key-value pair is represented as a Map.Entry. If the multimap allows duplicate values, a separate Map.Entry will be included for each duplicate value.- Specified by:
entries
in interfaceImmutableMultimap<K,
V> - Returns:
- a collection view of all key-value pairs contained in this multimap
-
size
int size()Returns the total number of key-value pairs in the multimap.- Specified by:
size
in interfaceImmutableMultimap<K,
V> - Returns:
- the total number of key-value pairs
-
copy
Creates a deep copy of this multimap.- Specified by:
copy
in interfaceImmutableMultimap<K,
V> - Returns:
- a new multimap instance containing the same key-value mappings as this multimap
-
inverse
Creates an inverse view of this multimap. In the inverse multimap, each value in the original multimap becomes a key in the new multimap, and each key in the original becomes part of the set of values associated with the new key. The inverse multimap is also immutable.- Specified by:
inverse
in interfaceImmutableMultimap<K,
V> - Returns:
- a new Multimap instance where the roles of keys and values are swapped
-