Class XCollectionUtils


  • public class XCollectionUtils
    extends Object
    Utility class for operations on collections and iterables.
    Since:
    7.0.0
    Author:
    XIMA Media GmbH
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T firstItem​(List<T> list)  
      static <T> T lastItem​(List<T> list)  
      static <T> List<T> listOfSize​(int targetSize, T initialValue)  
      static <T> List<T> listOfSizeWith​(int targetSize, IntFunction<T> initialValue)  
      static <K1,​K2,​V>
      Map<K2,​V>
      mapKeys​(Map<? extends K1,​? extends V> map, Function<? super K1,​? extends K2> keyMapper)
      Remaps the keys of a map by applying the given mapping function to all keys, returning a new map.
      static <K1,​K2,​V>
      Map<K2,​V>
      mapKeys​(Map<? extends K1,​? extends V> map, Function<? super K1,​? extends K2> keyMapper, BinaryOperator<V> mergeFunction)
      Remaps the keys of a map by applying the given mapping function to all keys, returning a new map.
      static <K1,​K2,​V>
      Map<K2,​V>
      mapKeysToUnmodifiableMap​(Map<? extends K1,​? extends V> map, Function<? super K1,​? extends K2> keyMapper)
      Remaps the keys of a map by applying the given mapping function to all keys, returning a new unmodifiable map.
      static <K1,​K2,​V>
      Map<K2,​V>
      mapKeysToUnmodifiableMap​(Map<? extends K1,​? extends V> map, Function<? super K1,​? extends K2> keyMapper, BinaryOperator<V> mergeFunction)
      Remaps the keys of a map by applying the given mapping function to all keys, returning a new unmodifiable map.
      static <K1,​V1,​K2,​V2>
      Map<K2,​V2>
      mapMap​(Map<? extends K1,​? extends V1> map, BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper)
      Maps the entries of a map by applying the given mapping function, returning a new map.
      static <K1,​V1,​K2,​V2>
      Map<K2,​V2>
      mapMap​(Map<? extends K1,​? extends V1> map, BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper, BinaryOperator<V2> mergeFunction)
      Maps the entries of a map by applying the given mapping function, returning a new map.
      static <K1,​V1,​K2,​V2,​M extends Map<K2,​V2>>
      M
      mapMap​(Map<? extends K1,​? extends V1> map, BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper, BinaryOperator<V2> mergeFunction, Supplier<M> mapFactory)
      Maps the entries of a map by applying the given mapping function, returning a new map.
      static <K1,​V1,​K2,​V2>
      Map<K2,​V2>
      mapToUnmodifiableMap​(Map<? extends K1,​? extends V1> map, BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper)
      Maps the entries of a map by applying the given mapping function, returning a new unmodifiable map.
      static <K1,​V1,​K2,​V2>
      Map<K2,​V2>
      mapToUnmodifiableMap​(Map<? extends K1,​? extends V1> map, BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper, BinaryOperator<V2> mergeFunction)
      Maps the entries of a map by applying the given mapping function, returning a new unmodifiable map.
      static <K,​V1,​V2>
      Map<K,​V2>
      mapValues​(Map<? extends K,​? extends V1> map, Function<? super V1,​? extends V2> valueMapper)
      Remaps the values of a map by applying the given mapping function to all keys, returning a new map.
      static <K,​V1,​V2>
      Map<K,​V2>
      mapValuesToUnmodifiableMap​(Map<? extends K,​? extends V1> map, Function<? super V1,​? extends V2> valueMapper)
      Remaps the values of a map by applying the given mapping function to all keys, returning a new unmodifiable map.
      static <T> List<T> serializableList​(Iterable<T> sequence)
      Takes any Collection or Iterable and converts it to a serializable list that can be transferred via the API.
    • Method Detail

      • firstItem

        public static <T> T firstItem​(List<T> list)
        Type Parameters:
        T - Type of the elements in the list.
        Parameters:
        list - List to process.
        Returns:
        The first item in the list, or null if the list is null or empty, or the first item is null.
      • lastItem

        public static <T> T lastItem​(List<T> list)
        Type Parameters:
        T - Type of the elements in the list.
        Parameters:
        list - List to process.
        Returns:
        The last item in the list, or null if the list is null or empty, or the last item is null.
      • listOfSize

        public static <T> List<T> listOfSize​(int targetSize,
                                             T initialValue)
        Type Parameters:
        T - Type of the list elements.
        Parameters:
        targetSize - Desired size of the list.
        initialValue - Initial value for each element.
        Returns:
        A list of the given size, with each element set to the given initial value.
      • listOfSizeWith

        public static <T> List<T> listOfSizeWith​(int targetSize,
                                                 IntFunction<T> initialValue)
        Type Parameters:
        T - Type of the list elements.
        Parameters:
        targetSize - Desired size of the list.
        initialValue - Supplier for the initial value for each element.
        Returns:
        A list of the given size, with each element set to the value returned by the given supplier.
      • mapKeys

        public static <K1,​K2,​V> Map<K2,​V> mapKeys​(Map<? extends K1,​? extends V> map,
                                                                    Function<? super K1,​? extends K2> keyMapper)
        Remaps the keys of a map by applying the given mapping function to all keys, returning a new map. If there are multiple mapped entries with the same key, the latter entry will be used.
        Type Parameters:
        K1 - Type of the input map keys.
        K2 - Type of the output map keys.
        V - Type of the map values.
        Parameters:
        map - Map to process.
        keyMapper - Mapping function for the keys.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapKeys

        public static <K1,​K2,​V> Map<K2,​V> mapKeys​(Map<? extends K1,​? extends V> map,
                                                                    Function<? super K1,​? extends K2> keyMapper,
                                                                    BinaryOperator<V> mergeFunction)
        Remaps the keys of a map by applying the given mapping function to all keys, returning a new map.
        Type Parameters:
        K1 - Type of the input map keys.
        K2 - Type of the output map keys.
        V - Type of the map values.
        Parameters:
        map - Map to process.
        keyMapper - Mapping function for the keys.
        mergeFunction - Merge function values that map to the same key.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapKeysToUnmodifiableMap

        public static <K1,​K2,​V> Map<K2,​V> mapKeysToUnmodifiableMap​(Map<? extends K1,​? extends V> map,
                                                                                     Function<? super K1,​? extends K2> keyMapper)
        Remaps the keys of a map by applying the given mapping function to all keys, returning a new unmodifiable map. If there are multiple mapped entries with the same key, the latter entry will be used.
        Type Parameters:
        K1 - Type of the input map keys.
        K2 - Type of the output map keys.
        V - Type of the map values.
        Parameters:
        map - Map to process.
        keyMapper - Mapping function for the keys.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapKeysToUnmodifiableMap

        public static <K1,​K2,​V> Map<K2,​V> mapKeysToUnmodifiableMap​(Map<? extends K1,​? extends V> map,
                                                                                     Function<? super K1,​? extends K2> keyMapper,
                                                                                     BinaryOperator<V> mergeFunction)
        Remaps the keys of a map by applying the given mapping function to all keys, returning a new unmodifiable map.
        Type Parameters:
        K1 - Type of the input map keys.
        K2 - Type of the output map keys.
        V - Type of the map values.
        Parameters:
        map - Map to process.
        keyMapper - Mapping function for the keys.
        mergeFunction - Merge function values that map to the same key.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapMap

        public static <K1,​V1,​K2,​V2> Map<K2,​V2> mapMap​(Map<? extends K1,​? extends V1> map,
                                                                              BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper)
        Maps the entries of a map by applying the given mapping function, returning a new map. If there are multiple mapped entries with the same key, the latter entry will be used.
        Type Parameters:
        K1 - Type of the input map keys.
        V1 - Type of the input map values.
        K2 - Type of the output map keys.
        V2 - Type of the output map values.
        Parameters:
        map - Map to process.
        entryMapper - Mapping function for the entries.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapMap

        public static <K1,​V1,​K2,​V2> Map<K2,​V2> mapMap​(Map<? extends K1,​? extends V1> map,
                                                                              BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper,
                                                                              BinaryOperator<V2> mergeFunction)
        Maps the entries of a map by applying the given mapping function, returning a new map.
        Type Parameters:
        K1 - Type of the input map keys.
        V1 - Type of the input map values.
        K2 - Type of the output map keys.
        V2 - Type of the output map values.
        Parameters:
        map - Map to process.
        entryMapper - Mapping function for the entries.
        mergeFunction - Merge function for duplicate entries.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapMap

        public static <K1,​V1,​K2,​V2,​M extends Map<K2,​V2>> M mapMap​(Map<? extends K1,​? extends V1> map,
                                                                                                BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper,
                                                                                                BinaryOperator<V2> mergeFunction,
                                                                                                Supplier<M> mapFactory)
        Maps the entries of a map by applying the given mapping function, returning a new map.
        Type Parameters:
        K1 - Type of the input map keys.
        V1 - Type of the input map values.
        K2 - Type of the output map keys.
        V2 - Type of the output map values.
        M - Type of the returned map.
        Parameters:
        map - Map to process.
        entryMapper - Mapping function for the entries.
        mergeFunction - Merge function for duplicate entries.
        mapFactory - Factory for the map to build.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapToUnmodifiableMap

        public static <K1,​V1,​K2,​V2> Map<K2,​V2> mapToUnmodifiableMap​(Map<? extends K1,​? extends V1> map,
                                                                                            BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper)
        Maps the entries of a map by applying the given mapping function, returning a new unmodifiable map. If there are multiple mapped entries with the same key, the latter entry will be used.
        Type Parameters:
        K1 - Type of the input map keys.
        V1 - Type of the input map values.
        K2 - Type of the output map keys.
        V2 - Type of the output map values.
        Parameters:
        map - Map to process.
        entryMapper - Mapping function for the entries.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapToUnmodifiableMap

        public static <K1,​V1,​K2,​V2> Map<K2,​V2> mapToUnmodifiableMap​(Map<? extends K1,​? extends V1> map,
                                                                                            BiFunction<? super K1,​? super V1,​? extends Map.Entry<? extends K2,​? extends V2>> entryMapper,
                                                                                            BinaryOperator<V2> mergeFunction)
        Maps the entries of a map by applying the given mapping function, returning a new unmodifiable map.
        Type Parameters:
        K1 - Type of the input map keys.
        V1 - Type of the input map values.
        K2 - Type of the output map keys.
        V2 - Type of the output map values.
        Parameters:
        map - Map to process.
        entryMapper - Mapping function for the entries.
        mergeFunction - Merge function values that map to the same key.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapValues

        public static <K,​V1,​V2> Map<K,​V2> mapValues​(Map<? extends K,​? extends V1> map,
                                                                      Function<? super V1,​? extends V2> valueMapper)
        Remaps the values of a map by applying the given mapping function to all keys, returning a new map.
        Type Parameters:
        K - Type of the map keys.
        V1 - Type of the input map values.
        V2 - Type of the output map values.
        Parameters:
        map - Map to process.
        valueMapper - Mapping function for the values.
        Returns:
        The new map.
        Since:
        8.0.0
      • mapValuesToUnmodifiableMap

        public static <K,​V1,​V2> Map<K,​V2> mapValuesToUnmodifiableMap​(Map<? extends K,​? extends V1> map,
                                                                                       Function<? super V1,​? extends V2> valueMapper)
        Remaps the values of a map by applying the given mapping function to all keys, returning a new unmodifiable map.
        Type Parameters:
        K - Type of the map keys.
        V1 - Type of the input map values.
        V2 - Type of the output map values.
        Parameters:
        map - Map to process.
        valueMapper - Mapping function for the values.
        Returns:
        The new map.
        Since:
        8.0.0
      • serializableList

        public static <T> List<T> serializableList​(Iterable<T> sequence)
        Takes any Collection or Iterable and converts it to a serializable list that can be transferred via the API.
        Type Parameters:
        T - Type of the list items.
        Parameters:
        sequence - A sequence of items to put into a list.
        Returns:
        A serializable list with the given items.