Class CollectionUtil


  • public final class CollectionUtil
    extends java.lang.Object
    Utilities for Collections.
    Author:
    Kim Rutherford, Matthew Wakeling
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> java.util.Collection<java.util.List<E>> fanOutCombinations​(java.util.List<java.util.Collection<E>> values)
      Returns a collection containing all possible combinations of the input values.
      static java.lang.Class<?> findCommonSuperclass​(java.util.Collection<java.lang.Class<?>> classes)
      Finds a common superclass or superinterface for all the classes specified in the arguments.
      static java.util.Set<java.lang.Class<?>> findCommonSuperclasses​(java.util.Collection<java.lang.Class<?>> classes)
      Finds common superclasses and superinterfaces for all the classes specified in the arguments.
      static <E> java.util.Map<java.lang.Class<?>,​java.util.List<E>> groupByClass​(java.util.Collection<E> objects, boolean inherit)
      Sorts objects in the given collection into different types by Class.
      static <K,​V>
      java.util.LinkedHashMap<K,​V>
      linkedHashMapAdd​(java.util.LinkedHashMap<K,​V> map, K prevKey, K newKey, V newValue)
      Return a copy of the given map with the object inserted at the given index.
      • Methods inherited from class java.lang.Object

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

      • linkedHashMapAdd

        public static <K,​V> java.util.LinkedHashMap<K,​V> linkedHashMapAdd​(java.util.LinkedHashMap<K,​V> map,
                                                                                      K prevKey,
                                                                                      K newKey,
                                                                                      V newValue)
        Return a copy of the given map with the object inserted at the given index.
        Type Parameters:
        K - The key type
        V - The value type
        Parameters:
        map - the LinkedHashMap to copy
        prevKey - the newKey,newValue pair are added after this key. If null the newKey,newValue pair is added first
        newKey - the new key
        newValue - the new value
        Returns:
        the copied LinkedHashMap with newKey and newValue added
      • groupByClass

        public static <E> java.util.Map<java.lang.Class<?>,​java.util.List<E>> groupByClass​(java.util.Collection<E> objects,
                                                                                                 boolean inherit)
        Sorts objects in the given collection into different types by Class.
        Type Parameters:
        E - the element type
        Parameters:
        objects - a Collection of objects
        inherit - if true, objects are put into all their class's superclasses as well, except Object - the original Collection can be used in that case
        Returns:
        a Map from Class to List of objects in that class
      • fanOutCombinations

        public static <E> java.util.Collection<java.util.List<E>> fanOutCombinations​(java.util.List<java.util.Collection<E>> values)
        Returns a collection containing all possible combinations of the input values.
        Type Parameters:
        E - the element type
        Parameters:
        values - a List of collections of values
        Returns:
        a collection of Lists, all the same size as the input List, containing all possible combinations of the values in the collections, in their respective indexes in the List.
      • findCommonSuperclasses

        public static java.util.Set<java.lang.Class<?>> findCommonSuperclasses​(java.util.Collection<java.lang.Class<?>> classes)
        Finds common superclasses and superinterfaces for all the classes specified in the arguments.
        Parameters:
        classes - a Collection of Classes
        Returns:
        a Collection of Classes that are superclasses of all the argument classes, without any that are superclasses of classes in this return Collection
      • findCommonSuperclass

        public static java.lang.Class<?> findCommonSuperclass​(java.util.Collection<java.lang.Class<?>> classes)
        Finds a common superclass or superinterface for all the classes specified in the arguments.
        Parameters:
        classes - a Collection of Classes
        Returns:
        a Class that is a common superclass or superinterface