Class CollectionUtil

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

public class CollectionUtil extends Object
Collections Util
Author:
Leonard Woo
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    static <T, E> 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.
    static <K, V, E> 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.
    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.
    static <K, V, E> Map<K,V>
    populateMapWithoutFilter(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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • CollectionUtil

      public CollectionUtil()
  • 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 <K, V, E> 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:
      K - the key type.
      V - the value type.
      E - the type of object contained in the Collection.
      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, E> 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:
      K - the key type.
      V - the value type.
      E - the type of object contained in the Collection.
      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 <T, E> 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:
      T - the type of object contained in the List
      E - the type of object contained in the Collection
      Parameters:
      elements - Collection object
      valueFunction - Object field function
      Returns:
      Field list
    • 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