Class 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
This can be used to easily configure multimap data structure implementations not provided
explicitly in com.google.common.collect
, for example:
ListMultimap<String, Integer> treeListMultimap =
MultimapBuilder.treeKeys().arrayListValues().build();
SetMultimap<Integer, MyEnum> hashEnumMultimap =
MultimapBuilder.hashKeys().enumSetValues(MyEnum.class).build();
MultimapBuilder
instances 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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
A specialization ofMultimapBuilder
that generatesListMultimap
instances.static class
An intermediate stage in aMultimapBuilder
in which the key-value collection map implementation has been specified, but the value collection implementation has not.static class
A specialization ofMultimapBuilder
that generatesSetMultimap
instances.static class
A specialization ofMultimapBuilder
that generatesSortedSetMultimap
instances. -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Returns a new, emptyMultimap
with the specified implementation.Returns aMultimap
with the specified implementation, initialized with the entries ofmultimap
.static <K0 extends Enum<K0>>
MultimapBuilder.MultimapBuilderWithKeys<K0> Uses anEnumMap
to map keys to value collections.hashKeys()
Uses aHashMap
to map keys to value collections.hashKeys
(int expectedKeys) Uses aHashMap
to map keys to value collections, initialized to expect the specified number of keys.Uses aLinkedHashMap
to map keys to value collections.linkedHashKeys
(int expectedKeys) Uses aLinkedHashMap
to map keys to value collections, initialized to expect the specified number of keys.treeKeys()
Uses a naturally-orderedTreeMap
to map keys to value collections.static <K0> MultimapBuilder.MultimapBuilderWithKeys
<K0> treeKeys
(Comparator<K0> comparator) Uses aTreeMap
sorted by the specified comparator to map keys to value collections.
-
Method Details
-
hashKeys
Uses aHashMap
to map keys to value collections. -
hashKeys
Uses aHashMap
to map keys to value collections, initialized to expect the specified number of keys.- Throws:
IllegalArgumentException
- ifexpectedKeys < 0
-
linkedHashKeys
Uses aLinkedHashMap
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
Uses aLinkedHashMap
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
Uses a naturally-orderedTreeMap
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
. -
treeKeys
Uses aTreeMap
sorted 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
comparator
is not serializable. -
enumKeys
public static <K0 extends Enum<K0>> MultimapBuilder.MultimapBuilderWithKeys<K0> enumKeys(Class<K0> keyClass) Uses anEnumMap
to map keys to value collections. -
build
Returns a new, emptyMultimap
with the specified implementation. -
build
Returns aMultimap
with the specified implementation, initialized with the entries ofmultimap
.
-