Class MultimapBuilder<K0 extends @Nullable java.lang.Object,V0 extends @Nullable java.lang.Object>
- java.lang.Object
-
- com.google.common.collect.MultimapBuilder<K0,V0>
-
- Type Parameters:
K0- An upper bound on the key type of the generated multimap.V0- An upper bound on the value type of the generated multimap.
- Direct Known Subclasses:
MultimapBuilder.ListMultimapBuilder,MultimapBuilder.SetMultimapBuilder
@GwtCompatible public abstract class MultimapBuilder<K0 extends @Nullable java.lang.Object,V0 extends @Nullable java.lang.Object> extends java.lang.Object
An immutable builder forMultimapinstances, letting you independently select the desired behaviors (for example, ordering) of the backing map and value-collections. Example:ListMultimap<UserId, ErrorResponse> errorsByUser = MultimapBuilder.linkedHashKeys().arrayListValues().build(); SortedSetMultimap<String, Method> methodsForName = MultimapBuilder.treeKeys().treeSetValues(this::compareMethods).build();MultimapBuilderinstances are immutable. Invoking a configuration method has no effect on the receiving instance; you must store and use the new builder instance it returns instead.The generated multimaps are serializable if the key and value types are serializable, unless stated otherwise in one of the configuration methods.
- Since:
- 16.0
- Author:
- Louis Wasserman
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMultimapBuilder.ListMultimapBuilder<K0 extends @Nullable java.lang.Object,V0 extends @Nullable java.lang.Object>A specialization ofMultimapBuilderthat generatesListMultimapinstances.static classMultimapBuilder.MultimapBuilderWithKeys<K0 extends @Nullable java.lang.Object>An intermediate stage in aMultimapBuilderin which the key-value collection map implementation has been specified, but the value collection implementation has not.static classMultimapBuilder.SetMultimapBuilder<K0 extends @Nullable java.lang.Object,V0 extends @Nullable java.lang.Object>A specialization ofMultimapBuilderthat generatesSetMultimapinstances.static classMultimapBuilder.SortedSetMultimapBuilder<K0 extends @Nullable java.lang.Object,V0 extends @Nullable java.lang.Object>A specialization ofMultimapBuilderthat generatesSortedSetMultimapinstances.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract <K extends K0,V extends V0>
Multimap<K,V>build()Returns a new, emptyMultimapwith the specified implementation.<K extends K0,V extends V0>
Multimap<K,V>build(Multimap<? extends K,? extends V> multimap)Returns aMultimapwith the specified implementation, initialized with the entries ofmultimap.static <K0 extends java.lang.Enum<K0>>
MultimapBuilder.MultimapBuilderWithKeys<K0>enumKeys(java.lang.Class<K0> keyClass)Uses anEnumMapto map keys to value collections.static MultimapBuilder.MultimapBuilderWithKeys<@Nullable java.lang.Object>hashKeys()Uses a hash table to map keys to value collections.static MultimapBuilder.MultimapBuilderWithKeys<@Nullable java.lang.Object>hashKeys(int expectedKeys)Uses a hash table to map keys to value collections, initialized to expect the specified number of keys.static MultimapBuilder.MultimapBuilderWithKeys<@Nullable java.lang.Object>linkedHashKeys()Uses a hash table to map keys to value collections.static MultimapBuilder.MultimapBuilderWithKeys<@Nullable java.lang.Object>linkedHashKeys(int expectedKeys)Uses an hash table to map keys to value collections, initialized to expect the specified number of keys.static MultimapBuilder.MultimapBuilderWithKeys<java.lang.Comparable>treeKeys()Uses a naturally-orderedTreeMapto map keys to value collections.static <K0 extends @Nullable java.lang.Object>
MultimapBuilder.MultimapBuilderWithKeys<K0>treeKeys(java.util.Comparator<K0> comparator)Uses aTreeMapsorted by the specified comparator to map keys to value collections.
-
-
-
Method Detail
-
hashKeys
public static MultimapBuilder.MultimapBuilderWithKeys<@Nullable java.lang.Object> hashKeys()
Uses a hash table to map keys to value collections.
-
hashKeys
public static MultimapBuilder.MultimapBuilderWithKeys<@Nullable java.lang.Object> hashKeys(int expectedKeys)
Uses a hash table to map keys to value collections, initialized to expect the specified number of keys.- Throws:
java.lang.IllegalArgumentException- ifexpectedKeys < 0
-
linkedHashKeys
public static MultimapBuilder.MultimapBuilderWithKeys<@Nullable java.lang.Object> linkedHashKeys()
Uses a hash table to map keys to value collections.The collections returned by
Multimap.keySet(),Multimap.keys(), andMultimap.asMap()will iterate through the keys in the order that they were first added to the multimap, save that if all values associated with a key are removed and then the key is added back into the multimap, that key will come last in the key iteration order.
-
linkedHashKeys
public static MultimapBuilder.MultimapBuilderWithKeys<@Nullable java.lang.Object> linkedHashKeys(int expectedKeys)
Uses an hash table to map keys to value collections, initialized to expect the specified number of keys.The collections returned by
Multimap.keySet(),Multimap.keys(), andMultimap.asMap()will iterate through the keys in the order that they were first added to the multimap, save that if all values associated with a key are removed and then the key is added back into the multimap, that key will come last in the key iteration order.
-
treeKeys
public static MultimapBuilder.MultimapBuilderWithKeys<java.lang.Comparable> treeKeys()
Uses a naturally-orderedTreeMapto map keys to value collections.The collections returned by
Multimap.keySet(),Multimap.keys(), andMultimap.asMap()will iterate through the keys in sorted order.For all multimaps generated by the resulting builder, the
Multimap.keySet()can be safely cast to aSortedSet, and theMultimap.asMap()can safely be cast to aSortedMap.
-
treeKeys
public static <K0 extends @Nullable java.lang.Object> MultimapBuilder.MultimapBuilderWithKeys<K0> treeKeys(java.util.Comparator<K0> comparator)
Uses aTreeMapsorted by the specified comparator to map keys to value collections.The collections returned by
Multimap.keySet(),Multimap.keys(), andMultimap.asMap()will iterate through the keys in sorted order.For all multimaps generated by the resulting builder, the
Multimap.keySet()can be safely cast to aSortedSet, and theMultimap.asMap()can safely be cast to aSortedMap.Multimaps generated by the resulting builder will not be serializable if
comparatoris not serializable.
-
enumKeys
public static <K0 extends java.lang.Enum<K0>> MultimapBuilder.MultimapBuilderWithKeys<K0> enumKeys(java.lang.Class<K0> keyClass)
Uses anEnumMapto map keys to value collections.- Since:
- 16.0
-
build
public abstract <K extends K0,V extends V0> Multimap<K,V> build()
Returns a new, emptyMultimapwith the specified implementation.
-
-