Class Grouping


  • public class Grouping
    extends java.lang.Object
    Utility class for algorithms that group collection elements together.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.List<java.util.List<java.lang.Integer>> groupIndices​(java.util.List<T> elements, java.util.function.BiPredicate<? super T,​? super T> equals)
      Group list elements according to the given equality predicate, storing the groups as lists of indices into the original list.
      static <K,​V>
      java.util.Map<K,​java.util.List<V>>
      groupZipMap​(java.util.List<K> keys, java.util.List<V> vals, java.util.function.BiPredicate<? super K,​? super K> equals)
      Construct a multimap from a list of keys and a list of values.
      static <K,​V>
      java.util.Map<K,​java.util.List<V>>
      groupZipMapFromIndices​(java.util.List<java.util.List<java.lang.Integer>> groupIndices, java.util.List<K> keys, java.util.List<V> vals)
      Construct a map from a grouped list of keys and a list of values.
      • Methods inherited from class java.lang.Object

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

      • groupIndices

        public static <T> java.util.List<java.util.List<java.lang.Integer>> groupIndices​(java.util.List<T> elements,
                                                                                         java.util.function.BiPredicate<? super T,​? super T> equals)
        Group list elements according to the given equality predicate, storing the groups as lists of indices into the original list.
        Type Parameters:
        T - type of list elements
        Parameters:
        elements - The list to work on.
        equals - The equality predicate to use. For best results, this should be an equivalence relation.
        Returns:
        the list of groups
      • groupZipMapFromIndices

        public static <K,​V> java.util.Map<K,​java.util.List<V>> groupZipMapFromIndices​(java.util.List<java.util.List<java.lang.Integer>> groupIndices,
                                                                                                  java.util.List<K> keys,
                                                                                                  java.util.List<V> vals)
        Construct a map from a grouped list of keys and a list of values. The key for each map entry is the first element of a group, and the value is a list of all values at the indices of the group.
        Type Parameters:
        K - type of keys
        V - type of values
        Parameters:
        groupIndices - Indices of group in keys
        keys - List of keys
        vals - List of values
        Returns:
        Multimap from keys to values
      • groupZipMap

        public static <K,​V> java.util.Map<K,​java.util.List<V>> groupZipMap​(java.util.List<K> keys,
                                                                                       java.util.List<V> vals,
                                                                                       java.util.function.BiPredicate<? super K,​? super K> equals)
        Construct a multimap from a list of keys and a list of values. The lists should be of equal length. Each key is mapped to the value at the same index, and keys are collated according to the given predicate.
        Type Parameters:
        K - type of keys
        V - type of values
        Parameters:
        keys - List of keys
        vals - List of values
        equals - The equality predicate to use. For best results, this should be an equivalence relation.
        Returns:
        Multimap from keys to values