java.lang.Object
org.seppiko.commons.utils.CollectionUtil
Collections Util
- Author:
- Leonard Woo
- See Also:
-
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 <E,T> 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 <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 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 <E,K, V> 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.static <K,V> Map<K, V> populateMapWithoutFilter(Collection<? extends V> elements, Function<? super V, ? extends K> keyFunction) Populates a Map using the suppliedFunctions to transform the elements into keys and values.static <E,T> Set<T> populateSet(Collection<? extends E> elements, Function<? super E, ? extends T> valueFunction) Populates a Set using the suppliedFunctions to transform the fields into values.static <T> voidSwap T list index a and b.static <T> T[]Convert List to Array.static <T> T[]Convert Set to Array.static <E> StringtoString(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.static <E> StringtoString(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.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.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.
-
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 <E,K, Map<K,V> 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:
E- the type of object contained in theCollection.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.valueFunction- theFunctionused to transform the element into a value.- Returns:
- the
Mapto 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 suppliedFunctions to transform the elements into keys and values.- 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.
-
populateMapWithoutFilter
public static <E,K, Map<K,V> 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:
E- the type of object contained in theCollection.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.valueFunction- theFunctionused to transform the element into a value.- Returns:
- the
Mapto 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 suppliedFunctions to transform the fields into values.- Type Parameters:
E- the type of object contained in theCollection.T- the type of object contained in theList.- 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 suppliedFunctions to transform the fields into values.- Type Parameters:
E- the type of object contained in theCollection.T- the type of object contained in theSet.- 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 newCollection, in encounter order.- Type Parameters:
T- the type of the input elements.C- the type of the resultingCollection.- Parameters:
collection- source collection object e.g.ListSetorMap.supplier- 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.
-
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 supportListandSet.- Type Parameters:
E- the type of the input elements.- Parameters:
elements- theCollectioncontaining 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 supportListandSet.- Type Parameters:
E- the type of the input elements.- Parameters:
elements- theCollectioncontaining 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- theMapcontaining the input KVs.keyFunction- key field function.valueFunction- value field function withtoString.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- theMapcontaining the input KVs.keyFunction- key field function.valueFunction- value field function withtoString.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
Swap T list index a and b.- Type Parameters:
T- Object type.- Parameters:
list- origin T list.a- first index.b- second index.
-
toArray
Convert List to Array.- Type Parameters:
T- Object type.- Parameters:
list- list.- Returns:
- array.
-
toArray
Convert Set to Array.- Type Parameters:
T- Object type.- Parameters:
set- set.- Returns:
- array.
-