Class CollectionUtil

java.lang.Object
org.seppiko.commons.utils.CollectionUtil

public class CollectionUtil extends Object
Collections Util
Author:
Leonard Woo
See Also:
  • Method Details

    • populateMap

      public static <K, V> Map<K,V> populateMap(Collection<? extends V> elements, Function<? super V,? extends K> keyFunction)
      Populates a Map using the supplied Function to transform the elements into keys, using the unaltered element as the value in the Map. If the key is duplicated, only the new key-value is retained.
      Type Parameters:
      K - the key type.
      V - the value type.
      Parameters:
      elements - the Collection containing the input values for the map.
      keyFunction - the Function used to transform the element into a key value.
      Returns:
      the Map to populate.
    • populateMap

      public static <E, K, V> Map<K,V> populateMap(Collection<? extends E> elements, Function<? super E,? extends K> keyFunction, Function<? super E,? extends V> valueFunction)
      Populates a Map using the supplied Functions to transform the elements into keys and values. If the key is duplicated, only the new key-value is retained.
      Type Parameters:
      E - the type of object contained in the Collection.
      K - the key type.
      V - the value type.
      Parameters:
      elements - the Collection containing the input values for the map.
      keyFunction - the Function used to transform the element into a key value.
      valueFunction - the Function used to transform the element into a value.
      Returns:
      the Map to populate.
    • populateMapWithoutFilter

      public static <K, V> Map<K,V> populateMapWithoutFilter(Collection<? extends V> elements, Function<? super V,? extends K> keyFunction)
      Populates a Map using the supplied Functions to transform the elements into keys and values.
      Type Parameters:
      K - the key type.
      V - the value type.
      Parameters:
      elements - the Collection containing the input values for the map.
      keyFunction - the Function used to transform the element into a key value.
      Returns:
      the Map to populate.
    • populateMapWithoutFilter

      public static <E, K, V> Map<K,V> populateMapWithoutFilter(Collection<? extends E> elements, Function<? super E,? extends K> keyFunction, Function<? super E,? extends V> valueFunction) throws IllegalStateException
      Populates a Map using the supplied Functions to transform the elements into keys and values.
      Type Parameters:
      E - the type of object contained in the Collection.
      K - the key type.
      V - the value type.
      Parameters:
      elements - the Collection containing the input values for the map.
      keyFunction - the Function used to transform the element into a key value.
      valueFunction - the Function used to transform the element into a value.
      Returns:
      the Map to populate.
      Throws:
      IllegalStateException - Duplicate keys.
    • populateList

      public static <E, T> List<T> populateList(Collection<? extends E> elements, Function<? super E,? extends T> valueFunction)
      Populates a List using the supplied Functions to transform the fields into values.
      Type Parameters:
      E - the type of object contained in the Collection.
      T - the type of object contained in the List.
      Parameters:
      elements - Collection object.
      valueFunction - Object field function.
      Returns:
      Field list.
    • populateSet

      public static <E, T> Set<T> populateSet(Collection<? extends E> elements, Function<? super E,? extends T> valueFunction)
      Populates a Set using the supplied Functions to transform the fields into values.
      Type Parameters:
      E - the type of object contained in the Collection.
      T - the type of object contained in the Set.
      Parameters:
      elements - Collection object.
      valueFunction - Object field function.
      Returns:
      Field set.
    • clone

      public static <T, C extends Collection<T>> Collection<T> clone(Collection<? extends T> collection, Supplier<C> supplier)
      Returns a clone that accumulates the input elements into a new Collection, in encounter order.
      Type Parameters:
      T - the type of the input elements.
      C - the type of the resulting Collection.
      Parameters:
      collection - source collection object e.g. List Set or Map.
      supplier - a supplier providing a new empty Collection into which the results will be inserted.
      Returns:
      a Collector which collects all the input elements into a Collection, in encounter order.
    • toString

      public static <E> String toString(Collection<E> elements, Function<? super E,String> elementFunction, CharSequence delimiter)
      Returns a Collector that concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix, in encounter order. Only support List and Set.
      Type Parameters:
      E - the type of the input elements.
      Parameters:
      elements - the Collection containing the input values.
      elementFunction - Object field function.
      delimiter - the delimiter to be used between each element.
      Returns:
      String
    • toString

      public static <E> String toString(Collection<E> elements, Function<? super E,String> elementFunction, CharSequence delimiter, CharSequence prefix, CharSequence suffix)
      Returns a Collector that concatenates the input elements, separated by the specified delimiter, with the specified prefix and suffix, in encounter order. Only support List and Set.
      Type Parameters:
      E - the type of the input elements.
      Parameters:
      elements - the Collection containing the input values.
      elementFunction - Object field function.
      delimiter - the delimiter to be used between each element.
      prefix - the sequence of characters to be used at the beginning of the joined result.
      suffix - the sequence of characters to be used at the end of the joined result.
      Returns:
      String
    • toString

      public static <K, V> String toString(Map<K,V> map, Function<? super K,String> keyFunction, Function<V,Object> valueFunction, CharSequence follower, CharSequence delimiter)
      Returns a Map that concatenates the input KVs(key/values), separated by the specified follower and delimiter, with the specified prefix and suffix, in encounter order.
      Type Parameters:
      K - the type of the input keys.
      V - the type of the input values.
      Parameters:
      map - the Map containing the input KVs.
      keyFunction - key field function.
      valueFunction - value field function with toString.
      follower - the follower to be used between Key and Value.
      delimiter - the delimiter to be used between each Key/Value.
      Returns:
      String
    • toString

      public static <K, V> String toString(Map<K,V> map, Function<? super K,String> keyFunction, Function<V,Object> valueFunction, CharSequence follower, CharSequence delimiter, CharSequence prefix, CharSequence suffix)
      Returns a Map that concatenates the input Key/Values, separated by the specified follower and delimiter, with the specified prefix and suffix, in encounter order.
      Type Parameters:
      K - the type of the input keys.
      V - the type of the input values.
      Parameters:
      map - the Map containing the input KVs.
      keyFunction - key field function.
      valueFunction - value field function with toString.
      follower - the follower to be used between Key and Value.
      delimiter - the delimiter to be used between each Key/Value.
      prefix - the sequence of characters to be used at the beginning of the joined result.
      suffix - the sequence of characters to be used at the end of the joined result.
      Returns:
      String
    • swap

      public static <T> void swap(List<T> list, int a, int b)
      Swap T list index a and b.
      Type Parameters:
      T - Object type.
      Parameters:
      list - origin T list.
      a - first index.
      b - second index.
    • toArray

      public static <T> T[] toArray(List<T> list)
      Convert List to Array.
      Type Parameters:
      T - Object type.
      Parameters:
      list - list.
      Returns:
      array.
    • toArray

      public static <T> T[] toArray(Set<T> set)
      Convert Set to Array.
      Type Parameters:
      T - Object type.
      Parameters:
      set - set.
      Returns:
      array.