Class GuavaCollectors


  • public class GuavaCollectors
    extends java.lang.Object
    Provides Collectors for Guava collection types.

    Supported container types:

    • com.google.common.base.Optional<T> (throws an exception if more than one row in result)
    • com.google.common.collect.ImmutableList<T>
    • com.google.common.collect.ImmutableSet<T>
    • com.google.common.collect.ImmutableSortedSet<T extends Comparable>

    Supported Maps and Multimaps types - for rows mapped to Map.Entry<K, V>:

    • com.google.common.collect.BiMap<K, V>
    • com.google.common.collect.ImmutableMap<K, V>
    • com.google.common.collect.Multimap<K, V>
    • com.google.common.collect.ListMultimap<K, V>
    • com.google.common.collect.ArrayListMultimap<K, V>
    • com.google.common.collect.LinkedListMultimap<K, V>
    • com.google.common.collect.SetMultimap<K, V>
    • com.google.common.collect.HashMultimap<K, V>
    • com.google.common.collect.TreeMultimap<K, V>
    • com.google.common.collect.ImmutableMultimap<K, V>
    • com.google.common.collect.ImmutableListMultimap<K, V>
    • com.google.common.collect.ImmutableSetMultimap<K, V>
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  GuavaCollectors.Factory  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private GuavaCollectors()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static <K,​V,​MB extends com.google.common.collect.ImmutableMultimap.Builder<K,​V>>
      MB
      combine​(MB a, MB b)  
      private static <K,​V,​M extends com.google.common.collect.Multimap<K,​V>>
      M
      combine​(M a, M b)  
      static CollectorFactory factory()
      Returns a CollectorFactory which knows how to create all supported Guava types.
      private static <K,​V,​M extends com.google.common.collect.Multimap<K,​V>>
      void
      putEntry​(M map, java.util.Map.Entry<K,​V> entry)  
      static <K,​V>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.ArrayListMultimap<K,​V>>
      toArrayListMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into an ArrayListMultimap<K, V>.
      static <K,​V>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.BiMap<K,​V>>
      toHashBiMap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a HashBiMap<K, V>.
      static <K,​V>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.HashMultimap<K,​V>>
      toHashMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a HashMultimap<K, V>.
      static <K,​V>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.ImmutableListMultimap<K,​V>>
      toImmutableListMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableListMultimap<K, V>.
      static <K,​V>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.ImmutableMap<K,​V>>
      toImmutableMap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableMap<K, V>.
      static <K,​V>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.ImmutableSetMultimap<K,​V>>
      toImmutableSetMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableSetMultimap<K, V>.
      static <K,​V>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.LinkedListMultimap<K,​V>>
      toLinkedListMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a LinkedListMultimap<K, V>.
      static <K,​V,​M extends com.google.common.collect.Multimap<K,​V>>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​M>
      toMultimap​(java.util.function.Supplier<M> multimapFactory)
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a Multimap<K, V> of the supplied type.
      static <T> java.util.stream.Collector<T,​?,​com.google.common.base.Optional<T>> toOptional()
      Returns a Collector that accumulates 0 or 1 input elements into Guava's Optional<T>.
      static <K extends java.lang.Comparable,​V extends java.lang.Comparable>
      java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.TreeMultimap<K,​V>>
      toTreeMultimap()
      Returns a Collector that accumulates Map.Entry<K, V> input elements into a TreeMultimap<K, V>.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • GuavaCollectors

        private GuavaCollectors()
    • Method Detail

      • factory

        public static CollectorFactory factory()
        Returns a CollectorFactory which knows how to create all supported Guava types.
        Returns:
        A CollectorFactory which knows how to create all supported Guava types
      • toOptional

        public static <T> java.util.stream.Collector<T,​?,​com.google.common.base.Optional<T>> toOptional()
        Returns a Collector that accumulates 0 or 1 input elements into Guava's Optional<T>. The returned collector will throw IllegalStateException whenever 2 or more elements are present in a stream. Null elements are mapped to Optional.absent().
        Type Parameters:
        T - the collected type
        Returns:
        a Collector which collects 0 or 1 input elements into a Guava Optional<T>.
      • toImmutableMap

        public static <K,​V> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.ImmutableMap<K,​V>> toImmutableMap()
        Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableMap<K, V>.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        Returns:
        a Collector which collects map entry elements into an ImmutableMap, in encounter order.
      • toHashBiMap

        public static <K,​V> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.BiMap<K,​V>> toHashBiMap()
        Returns a Collector that accumulates Map.Entry<K, V> input elements into a HashBiMap<K, V>. The returned collector will throw IllegalStateException whenever a set of input elements contains multiple entries with the same key.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        Returns:
        a Collector which collects map entry elements into a HashBiMap, in encounter order.
      • toImmutableListMultimap

        public static <K,​V> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.ImmutableListMultimap<K,​V>> toImmutableListMultimap()
        Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableListMultimap<K, V>.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        Returns:
        a Collector which collects map entry elements into an ImmutableListMultimap, in encounter order.
      • toImmutableSetMultimap

        public static <K,​V> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.ImmutableSetMultimap<K,​V>> toImmutableSetMultimap()
        Returns a Collector that accumulates Map.Entry<K, V> input elements into an ImmutableSetMultimap<K, V>.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        Returns:
        a Collector which collects map entry elements into an ImmutableSetMultimap, in encounter order.
      • toArrayListMultimap

        public static <K,​V> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.ArrayListMultimap<K,​V>> toArrayListMultimap()
        Returns a Collector that accumulates Map.Entry<K, V> input elements into an ArrayListMultimap<K, V>.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        Returns:
        a Collector which collects map entry elements into an ArrayListMultimap, in encounter order.
      • toLinkedListMultimap

        public static <K,​V> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.LinkedListMultimap<K,​V>> toLinkedListMultimap()
        Returns a Collector that accumulates Map.Entry<K, V> input elements into a LinkedListMultimap<K, V>.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        Returns:
        a Collector which collects map entry elements into a LinkedListMultimap, in encounter order.
      • toHashMultimap

        public static <K,​V> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.HashMultimap<K,​V>> toHashMultimap()
        Returns a Collector that accumulates Map.Entry<K, V> input elements into a HashMultimap<K, V>.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        Returns:
        a Collector which collects map entry elements into a ArrayListMultimap, in encounter order.
      • toTreeMultimap

        public static <K extends java.lang.Comparable,​V extends java.lang.Comparable> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​com.google.common.collect.TreeMultimap<K,​V>> toTreeMultimap()
        Returns a Collector that accumulates Map.Entry<K, V> input elements into a TreeMultimap<K, V>.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        Returns:
        a Collector which collects map entry elements into a TreeMultimap, in encounter order.
      • toMultimap

        public static <K,​V,​M extends com.google.common.collect.Multimap<K,​V>> java.util.stream.Collector<java.util.Map.Entry<K,​V>,​?,​M> toMultimap​(java.util.function.Supplier<M> multimapFactory)
        Returns a Collector that accumulates Map.Entry<K, V> input elements into a Multimap<K, V> of the supplied type.
        Type Parameters:
        K - the type of map keys
        V - the type of map values
        M - a supplier of your multimap type
        Parameters:
        multimapFactory - a Supplier which return a new, empty Multimap of the appropriate type.
        Returns:
        a Collector which collects map entry elements into a Multiamp, in encounter order.
      • putEntry

        private static <K,​V,​M extends com.google.common.collect.Multimap<K,​V>> void putEntry​(M map,
                                                                                                               java.util.Map.Entry<K,​V> entry)
      • combine

        private static <K,​V,​M extends com.google.common.collect.Multimap<K,​V>> M combine​(M a,
                                                                                                           M b)
      • combine

        private static <K,​V,​MB extends com.google.common.collect.ImmutableMultimap.Builder<K,​V>> MB combine​(MB a,
                                                                                                                              MB b)