|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.common.collect.Multimaps
public final class Multimaps
Provides static methods acting on or generating a Multimap
.
Method Summary | ||
---|---|---|
static
|
forMap(Map<K,V> map)
Returns a multimap view of the specified map. |
|
static
|
index(Iterable<? extends V> values,
Function<? super V,? extends K> keyFunction)
Creates an index ListMultimap that contains the results of applying
a specified function to each item in an Iterable of values. |
|
static
|
index(Iterable<? extends V> values,
Function<? super V,? extends K> keyFunction,
Multimap<K,V> multimap)
Indexes the specified values into a Multimap by applying a
specified function to each item in an Iterable of values. |
|
static
|
inverseArrayListMultimap(Multimap<K,V> multimap)
Creates an ArrayListMultimap that's the inverse of the provided
multimap. |
|
static
|
inverseHashMultimap(Multimap<K,V> multimap)
Creates a HashMultimap that's the inverse of the provided multimap. |
|
static
|
inverseTreeMultimap(Multimap<K,V> multimap)
Creates a TreeMultimap that's the inverse of the provided multimap. |
|
static
|
newArrayListMultimap()
Creates an empty ArrayListMultimap instance. |
|
static
|
newArrayListMultimap(Multimap<? extends K,? extends V> multimap)
Creates an ArrayListMultimap instance initialized with all elements
from the supplied Multimap . |
|
static
|
newHashMultimap()
Creates an empty HashMultimap instance. |
|
static
|
newHashMultimap(Multimap<? extends K,? extends V> multimap)
Creates a HashMultimap instance initialized with all elements from
the supplied Multimap . |
|
static
|
newLinkedHashMultimap()
Creates an empty LinkedHashMultimap instance. |
|
static
|
newLinkedHashMultimap(Multimap<? extends K,? extends V> multimap)
Creates a LinkedHashMultimap instance initialized with all elements
from the supplied Multimap . |
|
static
|
newListMultimap(Map<K,Collection<V>> map,
Supplier<? extends List<V>> factory)
Creates a new ListMultimap that uses the provided map and factory. |
|
static
|
newMultimap(Map<K,Collection<V>> map,
Supplier<? extends Collection<V>> factory)
Creates a new Multimap that uses the provided map and factory. |
|
static
|
newSetMultimap(Map<K,Collection<V>> map,
Supplier<? extends Set<V>> factory)
Creates a new SetMultimap that uses the provided map and factory. |
|
static
|
newSortedSetMultimap(Map<K,Collection<V>> map,
Supplier<? extends SortedSet<V>> factory)
Creates a new SortedSetMultimap that uses the provided map and
factory. |
|
static
|
newTreeMultimap()
Creates an empty TreeMultimap instance using the natural ordering
of keys and values. |
|
static
|
newTreeMultimap(Comparator<? super K> keyComparator,
Comparator<? super V> valueComparator)
Creates an empty TreeMultimap instance using explicit comparators. |
|
static
|
newTreeMultimap(Comparator<? super K> keyComparator,
Comparator<? super V> valueComparator,
Multimap<? extends K,? extends V> multimap)
Creates a TreeMultimap instance using explicit comparators,
initialized with all elements from the supplied Multimap . |
|
static
|
newTreeMultimap(Multimap<? extends K,? extends V> multimap)
Constructs a TreeMultimap with the same mappings as the specified
Multimap . |
|
static
|
synchronizedListMultimap(ListMultimap<K,V> multimap)
Returns a synchronized (thread-safe) ListMultimap backed by the
specified multimap. |
|
static
|
synchronizedMultimap(Multimap<K,V> multimap)
Returns a synchronized (thread-safe) multimap backed by the specified multimap. |
|
static
|
synchronizedSetMultimap(SetMultimap<K,V> multimap)
Returns a synchronized (thread-safe) SetMultimap backed by the
specified multimap. |
|
static
|
synchronizedSortedSetMultimap(SortedSetMultimap<K,V> multimap)
Returns a synchronized (thread-safe) SortedSetMultimap backed by
the specified multimap. |
|
static
|
unmodifiableListMultimap(ListMultimap<K,V> delegate)
Returns an unmodifiable view of the specified ListMultimap . |
|
static
|
unmodifiableMultimap(Multimap<K,V> delegate)
Returns an unmodifiable view of the specified multimap. |
|
static
|
unmodifiableSetMultimap(SetMultimap<K,V> delegate)
Returns an unmodifiable view of the specified SetMultimap . |
|
static
|
unmodifiableSortedSetMultimap(SortedSetMultimap<K,V> delegate)
Returns an unmodifiable view of the specified SortedSetMultimap . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static <K,V> HashMultimap<K,V> newHashMultimap()
HashMultimap
instance.
HashMultimap
public static <K,V> HashMultimap<K,V> newHashMultimap(Multimap<? extends K,? extends V> multimap)
HashMultimap
instance initialized with all elements from
the supplied Multimap
. If the supplied multimap contains duplicate
key-value pairs, those duplicate pairs will only be stored once in the new
multimap.
multimap
- the multimap whose contents are copied to this multimap.
HashMultimap
public static <K,V> ArrayListMultimap<K,V> newArrayListMultimap()
ArrayListMultimap
instance.
ArrayListMultimap
public static <K,V> ArrayListMultimap<K,V> newArrayListMultimap(Multimap<? extends K,? extends V> multimap)
ArrayListMultimap
instance initialized with all elements
from the supplied Multimap
.
multimap
- the multimap whose contents are copied to this multimap.
ArrayListMultimap
public static <K,V> LinkedHashMultimap<K,V> newLinkedHashMultimap()
LinkedHashMultimap
instance.
LinkedHashMultimap
public static <K,V> LinkedHashMultimap<K,V> newLinkedHashMultimap(Multimap<? extends K,? extends V> multimap)
LinkedHashMultimap
instance initialized with all elements
from the supplied Multimap
. If the supplied multimap contains
duplicate key-value pairs, those duplicate pairs will only be stored once
in the new multimap. The new multimap has the same
Multimap.entries()
iteration order as the input multimap, except
for excluding duplicate mappings.
multimap
- the multimap whose contents are copied to this multimap.
LinkedHashMultimap
public static <K extends Comparable,V extends Comparable> TreeMultimap<K,V> newTreeMultimap()
TreeMultimap
instance using the natural ordering
of keys and values. If the supplied multimap contains duplicate key-value
pairs, those duplicate pairs will only be stored once in the new multimap.
TreeMultimap
public static <K,V> TreeMultimap<K,V> newTreeMultimap(Multimap<? extends K,? extends V> multimap)
TreeMultimap
with the same mappings as the specified
Multimap
.
If the supplied multimap is an instance of TreeMultimap
, the
supplied multimap's comparators are copied to the new instance.
If the supplied multimap is not an instance of TreeMultimap
, the
new multimap is ordered using the natural ordering of the key and value
classes. The key and value classes must satisfy the Comparable
interface.
multimap
- the multimap whose contents are copied to this multimap.
TreeMultimap
public static <K,V> TreeMultimap<K,V> newTreeMultimap(@Nullable Comparator<? super K> keyComparator, @Nullable Comparator<? super V> valueComparator)
TreeMultimap
instance using explicit comparators.
keyComparator
- the comparator that determines the key ordering. If
it's null
, the natural ordering of the keys is used.valueComparator
- the comparator that determines the value ordering.
If it's null
, the natural ordering of the values is used.
TreeMultimap
public static <K,V> TreeMultimap<K,V> newTreeMultimap(@Nullable Comparator<? super K> keyComparator, @Nullable Comparator<? super V> valueComparator, Multimap<? extends K,? extends V> multimap)
TreeMultimap
instance using explicit comparators,
initialized with all elements from the supplied Multimap
.
multimap
- the multimap whose contents are copied to this multimap.
TreeMultimap
public static <K,V> Multimap<K,V> newMultimap(Map<K,Collection<V>> map, Supplier<? extends Collection<V>> factory)
Multimap
that uses the provided map and factory. It
can generate a multimap based on arbitrary Map
and
Collection
classes.
The factory
-generated and map
classes determine the
multimap iteration order. They also specify the behavior of the
equals
, hashCode
, and toString
methods for the
multimap and its returned views. However, the multimaps's get
method returns instances of a different class than factory.get()
does.
The multimap is serializable if map
, factory
, the
collections generated by factory
, and the multimap contents are all
serializable.
The multimap is not threadsafe when any concurrent operations update the
multimap, even if map
and the instances generated by
factory
are. Concurrent read operations will work correctly. To
allow concurrent update operations, wrap the multimap with a call to
synchronizedMultimap(com.google.common.collect.Multimap
.
Call this method only when the simpler methods
newArrayListMultimap()
, newHashMultimap()
,
newLinkedHashMultimap()
and newTreeMultimap()
won't
suffice.
map
- place to store the mapping from each key to its corresponding
valuesfactory
- supplier of new empty collections that will each hold all
values for a given key
IllegalArgumentException
- if map
is not emptypublic static <K,V> ListMultimap<K,V> newListMultimap(Map<K,Collection<V>> map, Supplier<? extends List<V>> factory)
ListMultimap
that uses the provided map and factory.
It can generate a multimap based on arbitrary Map
and List
classes.
The factory
-generated and map
classes determine the
multimap iteration order. They also specify the behavior of the
equals
, hashCode
, and toString
methods for the
multimap and its returned views. However, the multimaps's get
method returns instances of a different class than factory.get()
does.
The multimap is serializable if map
, factory
, the
lists generated by factory
, and the multimap contents are all
serializable.
The multimap is not threadsafe when any concurrent operations update the
multimap, even if map
and the instances generated by
factory
are. Concurrent read operations will work correctly. To
allow concurrent update operations, wrap the multimap with a call to
synchronizedListMultimap(com.google.common.collect.ListMultimap
.
Call this method only when the simpler method newArrayListMultimap()
won't suffice.
map
- place to store the mapping from each key to its corresponding
valuesfactory
- supplier of new empty lists that will each hold all values
for a given key
IllegalArgumentException
- if map
is not emptypublic static <K,V> SetMultimap<K,V> newSetMultimap(Map<K,Collection<V>> map, Supplier<? extends Set<V>> factory)
SetMultimap
that uses the provided map and factory.
It can generate a multimap based on arbitrary Map
and Set
classes.
The factory
-generated and map
classes determine the
multimap iteration order. They also specify the behavior of the
equals
, hashCode
, and toString
methods for the
multimap and its returned views. However, the multimaps's get
method returns instances of a different class than factory.get()
does.
The multimap is serializable if map
, factory
, the
sets generated by factory
, and the multimap contents are all
serializable.
The multimap is not threadsafe when any concurrent operations update the
multimap, even if map
and the instances generated by
factory
are. Concurrent read operations will work correctly. To
allow concurrent update operations, wrap the multimap with a call to
synchronizedSetMultimap(com.google.common.collect.SetMultimap
.
Call this method only when the simpler methods
newHashMultimap()
, newLinkedHashMultimap()
, and
newTreeMultimap()
won't suffice.
map
- place to store the mapping from each key to its corresponding
valuesfactory
- supplier of new empty sets that will each hold all values
for a given key
IllegalArgumentException
- if map
is not emptypublic static <K,V> SortedSetMultimap<K,V> newSortedSetMultimap(Map<K,Collection<V>> map, Supplier<? extends SortedSet<V>> factory)
SortedSetMultimap
that uses the provided map and
factory. It can generate a multimap based on arbitrary Map
and
SortedSet
classes.
The factory
-generated and map
classes determine the
multimap iteration order. They also specify the behavior of the
equals
, hashCode
, and toString
methods for the
multimap and its returned views. However, the multimaps's get
method returns instances of a different class than factory.get()
does.
The multimap is serializable if map
, factory
, the
sets generated by factory
, and the multimap contents are all
serializable.
The multimap is not threadsafe when any concurrent operations update the
multimap, even if map
and the instances generated by
factory
are. Concurrent read operations will work correctly. To
allow concurrent update operations, wrap the multimap with a call to
synchronizedSortedSetMultimap(com.google.common.collect.SortedSetMultimap
.
Call this method only when the simpler method newTreeMultimap()
won't suffice.
map
- place to store the mapping from each key to its corresponding
valuesfactory
- supplier of new empty sorted sets that will each hold
all values for a given key
IllegalArgumentException
- if map
is not emptypublic static <K,V> HashMultimap<V,K> inverseHashMultimap(Multimap<K,V> multimap)
HashMultimap
that's the inverse of the provided multimap.
If the input multimap includes the mapping from a key to a value, the
returned multimap contains a mapping from the value to the key.
If the input multimap has duplicate key-value mappings, the returned multimap includes the inverse mapping once.
The returned multimap is modifiable. Updating it will not affect the input multimap, and vice versa.
multimap
- the multimap to invert
public static <K,V> ArrayListMultimap<V,K> inverseArrayListMultimap(Multimap<K,V> multimap)
ArrayListMultimap
that's the inverse of the provided
multimap. If the input multimap includes the mapping from a key to a value,
the returned multimap contains a mapping from the value to the key.
The returned multimap is modifiable. Updating it will not affect the input multimap, and vice versa.
multimap
- the multimap to invert
public static <K extends Comparable,V extends Comparable> TreeMultimap<V,K> inverseTreeMultimap(Multimap<K,V> multimap)
TreeMultimap
that's the inverse of the provided multimap.
If the input multimap includes the mapping from a key to a value, the
returned multimap contains a mapping from the value to the key. The
returned multimap follows the natural ordering of its keys and values.
If the input multimap has duplicate key-value mappings, the returned multimap includes the inverse mapping once.
The returned multimap is modifiable. Updating it will not affect the input map, and vice versa. The returned multimap orders the keys and values according to their natural ordering.
multimap
- the multimap to invert
public static <K,V> Multimap<K,V> synchronizedMultimap(Multimap<K,V> multimap)
It is imperative that the user manually synchronize on the returned map when accessing any of its collection views:
Multimap<K,V> m = Multimaps.synchronizedMultimap( new HashMultimap<K,V>()); ... Set<K> s = m.keySet(); // Needn't be in synchronized block ... synchronized (m) { // Synchronizing on m, not s! Iterator<K> i = s.iterator(); // Must be in synchronized block while (i.hasNext()) { foo(i.next()); } }Failure to follow this advice may result in non-deterministic behavior.
Note that the generated multimap's Multimap.removeAll(java.lang.Object)
and
Multimap.replaceValues(K, java.lang.Iterable extends V>)
methods return collections that aren't
synchronized.
multimap
- the multimap to be wrapped in a synchronized view
public static <K,V> Multimap<K,V> unmodifiableMultimap(Multimap<K,V> delegate)
UnsupportedOperationException
.
Note that the generated multimap's Multimap.removeAll(java.lang.Object)
and
Multimap.replaceValues(K, java.lang.Iterable extends V>)
methods return collections that are
modifiable.
delegate
- the multimap for which an unmodifiable view is to be
returned
public static <K,V> SetMultimap<K,V> synchronizedSetMultimap(SetMultimap<K,V> multimap)
SetMultimap
backed by the
specified multimap.
You must follow the warnings described in synchronizedMultimap(com.google.common.collect.Multimap
.
multimap
- the multimap to be wrapped
public static <K,V> SetMultimap<K,V> unmodifiableSetMultimap(SetMultimap<K,V> delegate)
SetMultimap
. Query
operations on the returned multimap "read through" to the specified
multimap, and attempts to modify the returned multimap, either directly or
through the multimap's views, result in an
UnsupportedOperationException
.
Note that the generated multimap's Multimap.removeAll(java.lang.Object)
and
Multimap.replaceValues(K, java.lang.Iterable extends V>)
methods return collections that are
modifiable.
delegate
- the multimap for which an unmodifiable view is to be
returned
public static <K,V> SortedSetMultimap<K,V> synchronizedSortedSetMultimap(SortedSetMultimap<K,V> multimap)
SortedSetMultimap
backed by
the specified multimap.
You must follow the warnings described in synchronizedMultimap(com.google.common.collect.Multimap
.
multimap
- the multimap to be wrapped
public static <K,V> SortedSetMultimap<K,V> unmodifiableSortedSetMultimap(SortedSetMultimap<K,V> delegate)
SortedSetMultimap
.
Query operations on the returned multimap "read through" to the specified
multimap, and attempts to modify the returned multimap, either directly or
through the multimap's views, result in an
UnsupportedOperationException
.
Note that the generated multimap's Multimap.removeAll(java.lang.Object)
and
Multimap.replaceValues(K, java.lang.Iterable extends V>)
methods return collections that are
modifiable.
delegate
- the multimap for which an unmodifiable view is to be
returned
public static <K,V> ListMultimap<K,V> synchronizedListMultimap(ListMultimap<K,V> multimap)
ListMultimap
backed by the
specified multimap.
You must follow the warnings described in synchronizedMultimap(com.google.common.collect.Multimap
.
multimap
- the multimap to be wrapped
public static <K,V> ListMultimap<K,V> unmodifiableListMultimap(ListMultimap<K,V> delegate)
ListMultimap
. Query
operations on the returned multimap "read through" to the specified
multimap, and attempts to modify the returned multimap, either directly or
through the multimap's views, result in an
UnsupportedOperationException
.
Note that the generated multimap's Multimap.removeAll(java.lang.Object)
and
Multimap.replaceValues(K, java.lang.Iterable extends V>)
methods return collections that are
modifiable.
delegate
- the multimap for which an unmodifiable view is to be
returned
public static <K,V> SetMultimap<K,V> forMap(Map<K,V> map)
remove
operation, or through the setValue
operation on a map entry
returned by the iterator), the results of the iteration are undefined.
The multimap supports mapping removal, which removes the corresponding
mapping from the map. It does not support any operations which might add
mappings, such as put
, putAll
or replaceValues
.
The returned multimap will be serializable if the specified map is serializable.
map
- the backing map for the returned multimap viewpublic static <K,V> ListMultimap<K,V> index(Iterable<? extends V> values, Function<? super V,? extends K> keyFunction)
ListMultimap
that contains the results of applying
a specified function to each item in an Iterable
of values. Each
value will be stored as a value in the resulting multimap, yielding a
multimap with the same size as the input iterable. The key used to
store that value in the multimap will be the result of calling the function
on that value. The resulting multimap is created as an unmodifiable snapshot,
it does not reflect subsequent changes on the input iterable.
For example,
List<String> badGuys = Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde"); Function<String, Integer> stringLengthFunction = ...; Multimap<Integer, String> index = Multimaps.index(badGuys, stringLengthFunction); System.out.println(index);prints
{4=[Inky], 5=[Pinky, Pinky, Clyde], 6=[Blinky]}
values
- the values to use when constructing the ListMultimap
keyFunction
- the function used to produce the key for each value
ListMultimap
mapping the result of evaluating the function
keyFunction
on each value in the input collection to that valuepublic static <K,V> void index(Iterable<? extends V> values, Function<? super V,? extends K> keyFunction, Multimap<K,V> multimap)
Multimap
by applying a
specified function to each item in an Iterable
of values. Each
value will be stored as a value in the specified multimap. The key used to
store that value in the multimap will be the result of calling the function
on that value. Depending on the multimap implementation, duplicate entries
(equal keys and equal values) may be collapsed.
For example,
List<String> badGuys = Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde"); Function<String, Integer> stringLengthFunction = ...; Multimap<Integer, String> index = Multimaps.newHashMultimap(); Multimaps.index(badGuys, stringLengthFunction, index); System.out.println(index);prints
{4=[Inky], 5=[Pinky, Clyde], 6=[Blinky]}The
HashMultimap
collapses the duplicate occurrence of
(5, "Pinky")
.
values
- the values to add to the multimapkeyFunction
- the function used to produce the key for each valuemultimap
- the multimap to store the key value pairs
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |