Class CollectionTools


  • public final class CollectionTools
    extends Object
    Tool methods for collections
    • Method Detail

      • replaceWithEmptyOrCopy

        public static <K,​V> Map<K,​V> replaceWithEmptyOrCopy​(Map<K,​V> map)
        Replaces an empty map by the EMPTY_MAP constant if its size is zero, or copies its contents into a new HashMap.
        Parameters:
        map - The map to be either replaced by EMPTY_MAP or to be copied.
        Returns:
        The static EMPTY_MAP object or a fresh copy of the map's contents.
      • removeNullElements

        public static <T> List<T> removeNullElements​(Collection<? extends T> coll)
        Gets a list reflecting the designated collection but without null elements. If the collection itself is null, null will be returned.
        Parameters:
        coll - The collection for which to get a list but without null elements.
        Returns:
        A list reflecting the designated collection but without null elements (or null).
      • createArrayList

        public static <T> ArrayList<T> createArrayList​(int size)
        Creates a new array list of the designated initial size. If the size is ludicrously big, the default size will be used. This prevents problems with creating arrays which exceed the VM size for an array.
        Parameters:
        size - The initial size of the array list.
        Returns:
        An array with the designated initial size or the default size in case the designated size is too big.
      • transformColl

        public static <S,​T> HashSet<T> transformColl​(Collection<? extends S> sColl,
                                                           Function<S,​T> transformator)
        Transforms each object of the designated collection using the designated transformator. If an element of the collection is null, this will not be transformed but null will be used instead.
        Parameters:
        sColl - The collection of objects which to transform or null.
        transformator - The transformator which to apply to each element. This must not be null.
        Returns:
        A collection (set) with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformColl

        public static <S,​T> HashSet<T> transformColl​(Collection<? extends S> sColl,
                                                           Function<S,​T> transformator,
                                                           Supplier<T> nullSupplier)
        Transforms each object of the designated collection using the designated transformator. If an element of the collection is null, the designated nullSupplier (Supplier) will be called.
        Parameters:
        sColl - The collection of objects which to transform or null.
        transformator - The transformator which to apply to each element. This must not be null.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A collection (set) with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformList

        public static <S,​T> ArrayList<T> transformList​(Collection<? extends S> sColl,
                                                             Function<S,​T> transformator)
        Transforms each object of the designated collection using the designated transformator. If an element of the collection is null, this will not be transformed but null will be used instead.
        Parameters:
        sColl - The collection of objects which to transform or null.
        transformator - The transformator which to apply to each element. This must not be null.
        Returns:
        A list with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformList

        public static <S,​T> ArrayList<T> transformList​(Collection<? extends S> sColl,
                                                             Function<S,​T> transformator,
                                                             Supplier<T> nullSupplier)
        Transforms each object of the designated collection using the designated transformator. If an element of the collection is null, the designated nullSupplier (Supplier) will be called.
        Parameters:
        sColl - The collection of objects which to transform or null.
        transformator - The transformator which to apply to each element. This must not be null.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A list with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformColl

        protected static <S,​T,​C extends Collection<T>> C transformColl​(Collection<? extends S> sColl,
                                                                                   Function<Integer,​C> collCreator,
                                                                                   Function<S,​T> transformator,
                                                                                   Supplier<T> nullSupplier)
        Transforms each object of the designated collection using the designated transformator. If an element of the collection is null, the designated nullSupplier (Supplier) will be called. For creating the returned collection the designated collCreator (Function) will be used.
        Parameters:
        sColl - The collection of objects which to transform or null.
        collCreator - The creator for the returned collection accepting the collection size. This must not be null.
        transformator - The transformator which to apply to each element. This must not be null.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A collection (set) with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformIterable

        public static <S,​T> HashSet<T> transformIterable​(Iterable<? extends S> sIter,
                                                               Function<S,​T> transformator)
        Transforms each object of the designated Iterable using the designated transformator. If an element of the Iterable is null, this will not be transformed but null will be used instead.
        Parameters:
        sIter - The Iterable of objects which to transform or null.
        transformator - The transformator which to apply to each element. This must not be null.
        Returns:
        A collection (set) with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformIterable

        public static <S,​T> HashSet<T> transformIterable​(Iterable<? extends S> sIter,
                                                               Function<S,​T> transformator,
                                                               Supplier<T> nullSupplier)
        Transforms each object of the designated Iterable using the designated transformator. If an element of the Iterable is null, the designated nullSupplier (Supplier) will be called.
        Parameters:
        sIter - The Iterable of objects which to transform or null.
        transformator - The transformator which to apply to each element. This must not be null.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A collection (set) with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformList

        public static <S,​T> ArrayList<T> transformList​(Iterable<? extends S> sIter,
                                                             Function<S,​T> transformator)
        Transforms each object of the designated Iterable using the designated transformator. If an element of the Iterable is null, this will not be transformed but null will be used instead.
        Parameters:
        sIter - The Iterable of objects which to transform or null.
        transformator - The transformator which to apply to each element. This must not be null.
        Returns:
        A list with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformList

        public static <S,​T> ArrayList<T> transformList​(Iterable<? extends S> sIter,
                                                             Function<S,​T> transformator,
                                                             Supplier<T> nullSupplier)
        Transforms each object of the designated Iterable using the designated transformator. If an element of the Iterable is null, the designated nullSupplier (Supplier) will be called.
        Parameters:
        sIter - The Iterable of objects which to transform or null.
        transformator - The transformator which to apply to each element. This must not be null.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A list with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformIterable

        protected static <S,​T,​C extends Collection<T>> C transformIterable​(Iterable<? extends S> sIter,
                                                                                       Supplier<C> collCreator,
                                                                                       Function<S,​T> transformator,
                                                                                       Supplier<T> nullSupplier)
        Transforms each object of the designated Iterable using the designated transformator. If an element of the Iterable is null, the designated nullSupplier (Supplier) will be called. For creating the returned collection the designated collCreator (Supplier) will be used.
        Parameters:
        sIter - The Iterable of objects which to transform or null.
        collCreator - The creator for the returned collection. This must not be null.
        transformator - The transformator which to apply to each element. This must not be null.
        nullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A collection (set) with the transformed objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformMap

        public static <SK,​TK,​SV,​TV> HashMap<TK,​TV> transformMap​(Map<? extends SK,​? extends SV> sMap,
                                                                                        Function<SK,​TK> keyTrans,
                                                                                        Function<SV,​TV> valueTrans)
        Transforms each key and each value object of the designated map using the designated transformators. If a key or a value element of the collection is null, this will not be transformed but null used instead. Keys and values will be transformed separately.
        Parameters:
        sMap - The map of objects which to transform or null.
        keyTrans - The transformator which to apply to each key element. This must not be null.
        valueTrans - The transformator which to apply to each value element. This must not be null.
        Returns:
        A map with the transformed key and value objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformMap

        public static <SK,​TK,​SV,​TV> HashMap<TK,​TV> transformMap​(Map<? extends SK,​? extends SV> sMap,
                                                                                        Function<SK,​TK> keyTrans,
                                                                                        Function<SV,​TV> valueTrans,
                                                                                        Supplier<TK> keyNullSupplier,
                                                                                        Supplier<TV> valueNullSupplier)
        Transforms each key and each value object of the designated map using the designated transformators. If a key or a value element of the collection is null, this will not be transformed but null used instead. Keys and values will be transformed separately.
        Parameters:
        sMap - The map of objects which to transform or null.
        keyTrans - The transformator which to apply to each key element. This must not be null.
        valueTrans - The transformator which to apply to each value element. This must not be null.
        keyNullSupplier - The supplier providing the object to be used for null.
        valueNullSupplier - The supplier providing the object to be used for null.
        Returns:
        A map with the transformed key and value objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformMap

        public static <SK,​TK,​SV,​TV,​R extends Map<TK,​TV>> R transformMap​(Map<? extends SK,​? extends SV> sMap,
                                                                                                      Function<Integer,​R> creator,
                                                                                                      Function<SK,​TK> keyTrans,
                                                                                                      Function<SV,​TV> valueTrans)
        Transforms each key and each value object of the designated map using the designated transformators. If a key or a value element of the collection is null, this will not be transformed but null used instead. Keys and values will be transformed separately. The returned map will be created by the designated creator.
        Parameters:
        sMap - The map of objects which to transform or null.
        creator - The creator for the new map accepting the map size.
        keyTrans - The transformator which to apply to each key element. This must not be null.
        valueTrans - The transformator which to apply to each value element. This must not be null.
        Returns:
        A map with the transformed key and value objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformMap

        public static <SK,​TK,​SV,​TV,​R extends Map<TK,​TV>> R transformMap​(Map<? extends SK,​? extends SV> sMap,
                                                                                                      Function<Integer,​R> creator,
                                                                                                      Function<SK,​TK> keyTrans,
                                                                                                      Function<SV,​TV> valueTrans,
                                                                                                      Supplier<TK> keyNullSupplier,
                                                                                                      Supplier<TV> valueNullSupplier)
        Transforms each key and each value object of the designated map using the designated transformators. If a key or a value element of the collection is null, the corresponding supplier will be called. Keys and values will be transformed separately. The returned map will be created by the designated creator.
        Parameters:
        sMap - The map of objects which to transform or null.
        creator - The creator for the new map accepting the map size.
        keyTrans - The transformator which to apply to each key element. This must not be null.
        valueTrans - The transformator which to apply to each value element. This must not be null.
        keyNullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing key.
        valueNullSupplier - The supplier providing the object to be used for null. If this is null, null will be used as replacing value.
        Returns:
        A map with the transformed key and value objects or null.
        See Also:
        ObjectTools.transformNullSafe(Object, Function)
      • transformCollToMap

        public static <S,​TK,​TV> HashMap<TK,​TV> transformCollToMap​(Collection<S> sColl,
                                                                                    Function<S,​Pair<TK,​TV>> entryTrans)
        Transforms each entry of the designated collection to a map entry by transforming each element to an intermediate pair using the designated transformator. This transformator has to consider an entry being null. If it returns null, the corresponding element will be ignored. If it returns a pair containing null, this will be taken over. Make sure that all pairs have a unique first element!
        Parameters:
        sColl - The collection of objects which to transform or null.
        entryTrans - The transformator which to apply to each collection element. This must not be null. It has to handle collection elements being null.
        Returns:
        A map of entries being created by the designated transformator for every collection element or null.
      • transformCollToMap

        public static <S,​TK,​TV,​R extends Map<TK,​TV>> R transformCollToMap​(Collection<S> sColl,
                                                                                                  Function<Integer,​R> creator,
                                                                                                  Function<S,​Pair<TK,​TV>> entryTrans)
        Transforms each entry of the designated collection to a map entry by transforming each element to an intermediate pair using the designated transformator. This transformator has to consider an entry being null. If it returns null, the corresponding element will be ignored. If it returns a pair containing null, this will be taken over. Make sure that all pairs have a unique first element!
        The returned map will be created by the designated creator.
        Parameters:
        sColl - The collection of objects which to transform or null.
        creator - The creator for the new map accepting the map size.
        entryTrans - The transformator which to apply to each collection element. This must not be null. It has to handle collection elements being null.
        Returns:
        A map of entries being created by the designated transformator for every collection element or null.
      • transformIterableToMap

        public static <S,​TK,​TV> HashMap<TK,​TV> transformIterableToMap​(Iterable<S> sIter,
                                                                                        Function<S,​Pair<TK,​TV>> entryTrans)
        Transforms each element of the designated Iterable to a map entry by transforming each element to an intermediate pair using the designated transformator. This transformator has to consider an entry being null. If it returns null, the corresponding element will be ignored. If it returns a pair containing null, this will be taken over. Make sure that all pairs have a unique first element!
        Parameters:
        sIter - The Iterable of objects which to transform or null.
        entryTrans - The transformator which to apply to each Iterable element. This must not be null. It has to handle Iterable elements being null.
        Returns:
        A map of entries being created by the designated transformator for every Iterable element or null.
      • transformIterableToMap

        public static <S,​TK,​TV,​R extends Map<TK,​TV>> R transformIterableToMap​(Iterable<S> sIter,
                                                                                                      Supplier<R> creator,
                                                                                                      Function<S,​Pair<TK,​TV>> entryTrans)
        Transforms each element of the designated Iterable to a map entry by transforming each element to an intermediate pair using the designated transformator. This transformator has to consider an entry being null. If it returns null, the corresponding element will be ignored. If it returns a pair containing null, this will be taken over. Make sure that all pairs have a unique first element!
        The returned map will be created by the designated creator.
        Parameters:
        sIter - The Iterable of objects which to transform or null.
        creator - The creator for the new map.
        entryTrans - The transformator which to apply to each element of the Iterable. This must not be null. It has to handle Iterable elements being null.
        Returns:
        A map of entries being created by the designated transformator for every Iterable element or null.
      • transformMapToList

        public static <SK,​SV,​T> List<T> transformMapToList​(Map<SK,​SV> sMap,
                                                                       Function<Map.Entry<SK,​SV>,​T> entryTrans)
        Transforms each entry of the designated map to a list of instances of a combining type using the designated transformator. This transformator has to consider a key or a value element of the collection being null.
        Parameters:
        sMap - The map of objects which to transform or null.
        entryTrans - The transformator which to apply to each map entry element. This must not be null. It has to handle keys or values being null.
        Returns:
        A list of instances created by the designated transformator for every map entry or null.
      • sortCollection

        public static <T> Collection<T> sortCollection​(Collection<? extends T> unsorted,
                                                       Comparator<? super T> comp)
        Creates a new sorted collection containing the contents of the designated collection whereas sorting is delegated to the designated comparator.
        Parameters:
        unsorted - The collection containing the elements unsorted. This must not be null.
        comp - The comparator for sorting the elements. This must not be null.
        Returns:
        A sorted collection containing the designated elements.
      • contains

        public static <T> boolean contains​(T object,
                                           Enumeration<? extends T> enumeration)
        Gets whether the designated enumeration contains the designated object. This uses #equals(Object).
        Type Parameters:
        T - The type of object which to (not) find in the designated enumeration.
        Parameters:
        object - The object which to check for whether it is in the designated enumeration.
        enumeration - The enumeration which to check for the presence of the designated object.
        Returns:
        Whether the designated enumeration contains the designated object.
      • contains

        public static <T> boolean contains​(T object,
                                           Iterator<? extends T> iterator)
        Gets whether the designated iterator contains the designated object. This uses #equals(Object).
        Type Parameters:
        T - The type of object which to (not) find in the designated iterator.
        Parameters:
        object - The object which to check for whether it is in the designated iterator.
        iterator - The iterator which to check for the presence of the designated object.
        Returns:
        Whether the designated iterator contains the designated object.
      • identitySet

        public static <E> Set<E> identitySet()
        Gets a new set equalling elements based on object identity instead of Object.equals(Object).
        Type Parameters:
        E - The type of the elements of the set.
        Returns:
        A new set equalling elements based on object identity instead of Object.equals(Object).
      • identitySet

        public static <E> Set<E> identitySet​(int initialCapacity)
        Gets a new set equalling elements based on object identity instead of Object.equals(Object).
        Type Parameters:
        E - The type of the elements of the set.
        Parameters:
        initialCapacity - The initial capacity of the created set.
        Returns:
        A new set equalling elements based on object identity instead of Object.equals(Object).
      • permutate

        public static <T> Collection<Collection<T>> permutate​(Collection<T> toPerm,
                                                              boolean duplicates)
        Gets all permutations of the designated collection. Duplicates may be allowed or not, a duplicate is the "same" (with respect to the elements of the collection) collection as result of different permutations. This happens in case the designated permutation contains equal elements.
        Type Parameters:
        T - The type of elements of the collection.
        Parameters:
        toPerm - The elements which to permutate.
        duplicates - Whether the resulting (outer) collection should contain collections with the same element (order) or not. Duplicates occur in case the designated collection contains equal elements.
        Returns:
        All permutations of the designated collection, optionally containing duplicate collections.