Class CollectionUtils


  • public final class CollectionUtils
    extends Object
    • Method Detail

      • isNullOrEmpty

        public static boolean isNullOrEmpty​(Collection<?> collection)
      • isNullOrEmpty

        public static boolean isNullOrEmpty​(Map<?,​?> map)
      • isNotEmpty

        public static boolean isNotEmpty​(Map<?,​?> map)
      • mergeLists

        public static <T> List<T> mergeLists​(List<T> list1,
                                             List<T> list2)
        Returns a new list containing the second list appended to the first list.
      • firstIfPresent

        public static <T> T firstIfPresent​(List<T> list)
        Type Parameters:
        T - Type of elements in the list.
        Parameters:
        list - List to get first element from.
        Returns:
        The first element in the list if it exists. If the list is null or empty this will return null.
      • deepCopyMap

        public static <T,​U> Map<T,​List<U>> deepCopyMap​(Map<T,​? extends List<U>> map)
        Perform a deep copy of the provided map of lists. This only performs a deep copy of the map and lists. Entries are not copied, so care should be taken to ensure that entries are immutable if preventing unwanted mutations of the elements is desired.
      • deepCopyMap

        public static <T,​U> Map<T,​List<U>> deepCopyMap​(Map<T,​? extends List<U>> map,
                                                                   Supplier<Map<T,​List<U>>> mapConstructor)
        Perform a deep copy of the provided map of lists. This only performs a deep copy of the map and lists. Entries are not copied, so care should be taken to ensure that entries are immutable if preventing unwanted mutations of the elements is desired.
      • unmodifiableMapOfLists

        public static <T,​U> Map<T,​List<U>> unmodifiableMapOfLists​(Map<T,​List<U>> map)
      • toMap

        public static <K,​V> Collector<Map.Entry<K,​V>,​?,​Map<K,​V>> toMap()
        Collect a stream of Map.Entry to a Map with the same key/value types
        Type Parameters:
        K - the key type
        V - the value type
        Returns:
        a map
      • mapValues

        public static <K,​VInT,​VOutT> Map<K,​VOutT> mapValues​(Map<K,​VInT> inputMap,
                                                                              Function<VInT,​VOutT> mapper)
        Transforms the values of a map to another map with the same keys, using the supplied function.
        Type Parameters:
        K - the key type
        VInT - the value type for the input map
        VOutT - the value type for the output map
        Parameters:
        inputMap - the input map
        mapper - the function used to transform the map values
        Returns:
        a map
      • filterMap

        public static <K,​V> Map<K,​V> filterMap​(Map<K,​V> map,
                                                           Predicate<Map.Entry<K,​V>> condition)
        Filters a map based on a condition
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        map - the input map
        condition - the predicate to filter on
        Returns:
        the filtered map
      • inverseMap

        public static <K,​V> Map<K,​V> inverseMap​(Map<V,​K> inputMap)
        Return a new map that is the inverse of the supplied map, with the values becoming the keys and vice versa. Requires the values to be unique.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        inputMap - a map where both the keys and values are unique
        Returns:
        a map
      • uniqueIndex

        public static <K,​V> Map<K,​V> uniqueIndex​(Iterable<V> values,
                                                             Function<? super V,​K> indexFunction)
        For a collection of values of type V that can all be converted to type K, create a map that indexes all of the values by K. This requires that no two values map to the same index.
        Type Parameters:
        K - the index (or key) type
        V - the value type
        Parameters:
        values - the collection of values to index
        indexFunction - the function used to convert a value to its index
        Returns:
        a (modifiable) map that indexes K to its unique value V
        Throws:
        IllegalArgumentException - if any of the values map to the same index