public final class CollectionUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T,K> java.util.Map<K,T> |
collectionToMap(java.util.Collection<T> collection,
java.util.function.Function<? super T,? extends K> keyMapper)
Same as
collectionToMap(Collection, Function, Function), but uses entries themselves as map values. |
static <T,K,V> java.util.Map<K,V> |
collectionToMap(java.util.Collection<T> collection,
java.util.function.Function<? super T,? extends K> keyMapper,
java.util.function.Function<? super T,? extends V> valueMapper)
Convert a
collection to a map using keyMapper and valueMapper mappers. |
static <T,K> java.util.Set<K> |
collectionToSet(java.util.Collection<T> collection,
java.util.function.Function<? super T,? extends K> keyMapper)
Convert a
collection to a set of values using keyMapper mapping. |
static <T> java.util.Collection<T> |
emptyIfNull(java.util.Collection<T> collection)
Safe wrapper around nullable collection instances: returns
collection argument itself,
if the collection is non-null, otherwise returns (immutable) empty collection. |
static <T> java.util.List<T> |
emptyIfNull(java.util.List<T> list)
Safe wrapper around nullable list instances: returns
list argument itself,
if the list is non-null, otherwise returns (immutable) empty list. |
static <K,V> java.util.Map<K,V> |
emptyIfNull(java.util.Map<K,V> map)
Safe wrapper around nullable map instances: returns
map argument itself, if the map is non-null,
otherwise returns (immutable) empty map. |
static <T> java.util.Set<T> |
emptyIfNull(java.util.Set<T> set)
Safe wrapper around nullable set instances: returns
set argument itself,
if the set is non-null, otherwise returns (immutable) empty set. |
static <T> java.util.stream.Stream<T> |
filterCollection(java.util.Collection<T> collection,
java.util.function.Predicate<T> includeCondition)
Create a new collection which contains only elements which satisfy
includeCondition predicate. |
@Nonnull
public static <T> java.util.stream.Stream<T> filterCollection(@Nullable
java.util.Collection<T> collection,
@Nonnull
java.util.function.Predicate<T> includeCondition)
includeCondition predicate.T - type of the collection items.collection - Collection to be filteredincludeCondition - condition which verifies whether the value should be included to the final result.includeCondition predicate. If collection
is null or empty - empty stream is returned.@Nonnull
public static <T,K> java.util.Set<K> collectionToSet(@Nullable
java.util.Collection<T> collection,
@Nonnull
java.util.function.Function<? super T,? extends K> keyMapper)
collection to a set of values using keyMapper mapping.
If the collection has duplicate keys - only one will be stored, which one is not defined though.
T - type of collection entriesK - type of Set keycollection - Collection to convertkeyMapper - function which converts the collection entry to a key.Set which consists of items, converted from <T> entries to keys using
entryToKey function.@Nonnull
public static <T,K,V> java.util.Map<K,V> collectionToMap(@Nullable
java.util.Collection<T> collection,
@Nonnull
java.util.function.Function<? super T,? extends K> keyMapper,
@Nonnull
java.util.function.Function<? super T,? extends V> valueMapper)
collection to a map using keyMapper and valueMapper mappers.
If keys are duplicated - only one value is stored, which one - undefined.T - type of collection entriesK - type of Map keyV - type of Map valuecollection - Collection to convertkeyMapper - function which converts the collection entry to a keyvalueMapper - function which converts the collection entry to a valueMap which consists of key-value pairs, converted from <T> entriescollectionToMap(Collection, Function)@Nonnull
public static <T,K> java.util.Map<K,T> collectionToMap(@Nullable
java.util.Collection<T> collection,
@Nonnull
java.util.function.Function<? super T,? extends K> keyMapper)
collectionToMap(Collection, Function, Function), but uses entries themselves as map values.T - type of collection entriesK - type of Map keycollection - Collection to convertkeyMapper - function which converts the collection entry to a keyMap which consists of key-value pairs, converted from <T> entriescollectionToMap(Collection, Function, Function)@Nonnull
public static <T> java.util.Collection<T> emptyIfNull(@Nullable
java.util.Collection<T> collection)
collection argument itself,
if the collection is non-null, otherwise returns (immutable) empty collection.T - collection entities typecollection - Collection instance to processcollection instance, if non-null; otherwise immutable empty collection instance.emptyIfNull(List),
emptyIfNull(Set),
emptyIfNull(Map)@Nonnull
public static <T> java.util.List<T> emptyIfNull(@Nullable
java.util.List<T> list)
list argument itself,
if the list is non-null, otherwise returns (immutable) empty list.T - list entities typelist - List instance to processlist instance, if non-null; otherwise immutable empty list instance.emptyIfNull(Collection),
emptyIfNull(Set),
emptyIfNull(Map)@Nonnull
public static <T> java.util.Set<T> emptyIfNull(@Nullable
java.util.Set<T> set)
set argument itself,
if the set is non-null, otherwise returns (immutable) empty set.T - set entities typeset - Set instance to processset instance, if non-null; otherwise immutable empty set instance.emptyIfNull(Collection),
emptyIfNull(List),
emptyIfNull(Map)@Nonnull
public static <K,V> java.util.Map<K,V> emptyIfNull(@Nullable
java.util.Map<K,V> map)
map argument itself, if the map is non-null,
otherwise returns (immutable) empty map.K - map key typeV - map value typemap - Map instance to processmap instance, if non-null; otherwise immutable empty map instance.emptyIfNull(Collection),
emptyIfNull(List),
emptyIfNull(Set)