java.lang.Object
org.seppiko.commons.utils.CollectionUtil
Collections Util
- Author:
- Leonard Woo
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <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 newCollection, in encounter order.static <T,E> List<T> populateList(Collection<? extends E> elements, Function<? super E, ? extends T> valueFunction) Populates a List using the suppliedFunctions 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 suppliedFunctions 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 suppliedFunctionto transform the elements into keys, using the unaltered element as the value in theMap.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 suppliedFunctions to transform the elements into keys and values.
-
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 suppliedFunctionto transform the elements into keys, using the unaltered element as the value in theMap. If the key is duplicated, only the new key-value is retained.- Type Parameters:
K- the key type.V- the value type.- Parameters:
elements- theCollectioncontaining the input values for the map.keyFunction- theFunctionused to transform the element into a key value.- Returns:
- the
Mapto populate.
-
populateMap
public static <K,V, Map<K,E> V> populateMap(Collection<? extends E> elements, Function<? super E, ? extends K> keyFunction, Function<? super E, ? extends V> valueFunction) Populates a Map using the suppliedFunctions 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 theCollection.- Parameters:
elements- theCollectioncontaining the input values for the map.keyFunction- theFunctionused to transform the element into a key value.valueFunction- theFunctionused to transform the element into a value.- Returns:
- the
Mapto populate.
-
populateMapWithoutFilter
public static <K,V, Map<K,E> V> populateMapWithoutFilter(Collection<? extends E> elements, Function<? super E, ? extends K> keyFunction, Function<? super E, throws IllegalStateException? extends V> valueFunction) Populates a Map using the suppliedFunctions 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 theCollection.- Parameters:
elements- theCollectioncontaining the input values for the map.keyFunction- theFunctionused to transform the element into a key value.valueFunction- theFunctionused to transform the element into a value.- Returns:
- the
Mapto 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 suppliedFunctions to transform the fields into values.- Type Parameters:
T- the type of object contained in theListE- the type of object contained in theCollection- Parameters:
elements- Collection objectvalueFunction- 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 newCollection, in encounter order.- Type Parameters:
T- the type of the input elementsC- the type of the resultingCollection- Parameters:
collection- source collection object e.g.ListSetorMapsupplier- a supplier providing a new emptyCollectioninto which the results will be inserted- Returns:
- a
Collectorwhich collects all the input elements into aCollection, in encounter order
-