Class CollectionUtils


  • public final class CollectionUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T,​K>
      java.util.Map<K,​T>
      collectionToMap​(java.util.Collection<T> collection, java.util.function.Function<? super T,​? extends K> keyMapper)
      Same as collectionToMap(Collection, Function, Function), but uses entries themselves as map values.
      static <T,​K,​V>
      java.util.Map<K,​V>
      collectionToMap​(java.util.Collection<T> collection, java.util.function.Function<? super T,​? extends K> keyMapper, java.util.function.Function<? super T,​? extends V> valueMapper)
      Convert a collection to a map using keyMapper and valueMapper mappers.
      static <T> java.util.Collection<T> emptyIfNull​(java.util.Collection<T> collection)
      Safe wrapper around nullable collection instances: returns collection argument itself, if the collection is non-null, otherwise returns (immutable) empty collection.
      static <T> java.util.List<T> emptyIfNull​(java.util.List<T> list)
      Safe wrapper around nullable list instances: returns list argument itself, if the list is non-null, otherwise returns (immutable) empty list.
      static <T> java.util.stream.Stream<T> filterCollection​(java.util.Collection<T> collection, java.util.function.Predicate<T> includeCondition)
      Create a new collection which contains only elements which satisfy includeCondition predicate.
      • Methods inherited from class java.lang.Object

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

      • filterCollection

        @Nonnull
        public static <T> java.util.stream.Stream<T> filterCollection​(@Nullable
                                                                      java.util.Collection<T> collection,
                                                                      @Nonnull
                                                                      java.util.function.Predicate<T> includeCondition)
        Create a new collection which contains only elements which satisfy includeCondition predicate.
        Type Parameters:
        T - type of the collection items.
        Parameters:
        collection - Collection to be filtered
        includeCondition - condition which verifies whether the value should be included to the final result.
        Returns:
        new filtered stream with items which satisfy includeCondition predicate. If collection is null or empty - empty stream is returned.
      • collectionToMap

        @Nonnull
        public static <T,​K,​V> java.util.Map<K,​V> collectionToMap​(@Nullable
                                                                                   java.util.Collection<T> collection,
                                                                                   @Nonnull
                                                                                   java.util.function.Function<? super T,​? extends K> keyMapper,
                                                                                   @Nonnull
                                                                                   java.util.function.Function<? super T,​? extends V> valueMapper)
        Convert a collection to a map using keyMapper and valueMapper mappers. If keys are duplicated - only one value is stored, which one - undefined.
        Type Parameters:
        T - type of collection entries
        K - type of Map key
        V - type of Map value
        Parameters:
        collection - Collection to convert
        keyMapper - function which converts the collection entry to a key
        valueMapper - function which converts the collection entry to a value
        Returns:
        new Map which consists of key-value pairs, converted from <T> entries
        See Also:
        collectionToMap(Collection, Function)
      • collectionToMap

        @Nonnull
        public static <T,​K> java.util.Map<K,​T> collectionToMap​(@Nullable
                                                                           java.util.Collection<T> collection,
                                                                           @Nonnull
                                                                           java.util.function.Function<? super T,​? extends K> keyMapper)
        Same as collectionToMap(Collection, Function, Function), but uses entries themselves as map values.
        Type Parameters:
        T - type of collection entries
        K - type of Map key
        Parameters:
        collection - Collection to convert
        keyMapper - function which converts the collection entry to a key
        Returns:
        new Map which consists of key-value pairs, converted from <T> entries
        See Also:
        collectionToMap(Collection, Function, Function)
      • emptyIfNull

        @Nonnull
        public static <T> java.util.Collection<T> emptyIfNull​(@Nullable
                                                              java.util.Collection<T> collection)
        Safe wrapper around nullable collection instances: returns collection argument itself, if the collection is non-null, otherwise returns (immutable) empty collection.
        Type Parameters:
        T - collection entities type
        Parameters:
        collection - Collection instance to process
        Returns:
        original collection instance, if non-null; otherwise immutable empty collection instance.
        See Also:
        emptyIfNull(List)
      • emptyIfNull

        @Nonnull
        public static <T> java.util.List<T> emptyIfNull​(@Nullable
                                                        java.util.List<T> list)
        Safe wrapper around nullable list instances: returns list argument itself, if the list is non-null, otherwise returns (immutable) empty list.
        Type Parameters:
        T - list entities type
        Parameters:
        list - List instance to process
        Returns:
        original list instance, if non-null; otherwise immutable empty list instance.
        See Also:
        emptyIfNull(Collection)