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

    @GwtCompatible
    public abstract class MultimapBuilder<K0,​V0>
    extends 
    A builder for a multimap implementation that allows customization of the backing map and value collection implementations used in a particular multimap.

    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
    Author:
    Louis Wasserman
    • Method Detail

      • hashKeys

        public static > hashKeys()
        Uses a hash table to map keys to value collections.
      • hashKeys

        public static > hashKeys​(int expectedKeys)
        Uses a hash table to map keys to value collections, initialized to expect the specified number of keys.
        Throws:
        - if expectedKeys < 0
      • linkedHashKeys

        public static > linkedHashKeys()
        Uses a hash table to map keys to value collections.

        The collections returned by Multimap.keySet(), Multimap.keys(), and Multimap.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 > 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(), and Multimap.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 <K0> <K0> comparator)
        Uses a sorted by the specified comparator to map keys to value collections.

        The collections returned by Multimap.keySet(), Multimap.keys(), and Multimap.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 a , and the Multimap.asMap() can safely be cast to a .

        Multimaps generated by the resulting builder will not be serializable if comparator is not serializable.

      • enumKeys

        public static <K0 extends <K0>> <K0> keyClass)
        Uses an to map keys to value collections.
        Since:
        16.0
      • build

        public abstract <K extends K0,​V extends V0Multimap<K,​V> build()
        Returns a new, empty Multimap with the specified implementation.
      • build

        public <K extends K0,​V extends V0Multimap<K,​V> build​(Multimap<? extends K,​? extends V> multimap)
        Returns a Multimap with the specified implementation, initialized with the entries of multimap.