|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.ImmutableMultimapBuilder<K,V>
public class ImmutableMultimapBuilder<K,V>
A convenient way to populate immutable Multimap instances, especially static-final "constant Multimaps". Code such as
static final Multimap<String,Integer> STRING_TO_INTEGER_MULTIMAP
= createNumbersMap();
static Multimap<String,Integer> createNumbersMap() {
Multimap<String,Integer> multimap = Multimaps.newHashMultimap();
multimap.put("one", 1);
multimap.putAll("several", Arrays.asList(1, 2, 3));
multimap.putAll("many", Arrays.asList(1, 2, 3, 4, 5));
return Multimaps.unmodifiableMultimap(multimap);
}
... can be rewritten far more simply as ...
static final Multimap<String,Integer> STRING_TO_INTEGER_MULTIMAP
= new ImmutableMultimapBuilder<String,Integer>()
.put("one", 1)
.putAll("several", 1, 2, 3)
.putAll("many", 1, 2, 3, 4, 5)
.getMultimap();
The generated multimap is a ListMultimap, which allows duplicate
key-value pairs and maintains the value ordering for each key.
| Constructor Summary | |
|---|---|
ImmutableMultimapBuilder()
Creates a new ImmutableMultimapBuilder |
|
| Method Summary | |
|---|---|
ListMultimap<K,V> |
getMultimap()
Returns a newly-created, immutable Multimap instance containing the keys and values that were specified using put and putAll. |
ImmutableMultimapBuilder<K,V> |
put(K key,
V value)
Adds a key-value mapping to the multimap that will be returned by getMultimap. |
ImmutableMultimapBuilder<K,V> |
putAll(K key,
java.lang.Iterable<? extends V> values)
Stores a collection of values with the same key into the multimap that will be returned by getMultimap. |
ImmutableMultimapBuilder<K,V> |
putAll(K key,
V... values)
Stores an array of values with the same key into the multimap that will be returned by getMultimap. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ImmutableMultimapBuilder()
| Method Detail |
|---|
public ImmutableMultimapBuilder<K,V> put(@Nullable
K key,
@Nullable
V value)
getMultimap.
key - key with which the specified value is to be associatedvalue - value to be associated with the specified key
java.lang.IllegalStateException - if getMultimap has already been
called
public ImmutableMultimapBuilder<K,V> putAll(@Nullable
K key,
java.lang.Iterable<? extends V> values)
getMultimap.
key - key to store in the multimap.values - values to store in the multimap.
java.lang.IllegalStateException - if getMultimap has already been
called
public ImmutableMultimapBuilder<K,V> putAll(@Nullable
K key,
V... values)
getMultimap.
key - key to store in the multimap.values - values to store in the multimap.
java.lang.IllegalStateException - if getMultimap has already been
calledpublic ListMultimap<K,V> getMultimap()
put and putAll.
ListMultimap instance
java.lang.IllegalStateException - if getMultimap has already been
called
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||