Class CollectionUtils
- java.lang.Object
-
- com.commercetools.sync.commons.utils.CollectionUtils
-
public final class CollectionUtils extends java.lang.Object
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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 ascollectionToMap(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 acollectionto a map usingkeyMapperandvalueMappermappers.static <T,K>
java.util.Set<K>collectionToSet(java.util.Collection<T> collection, java.util.function.Function<? super T,? extends K> keyMapper)Convert acollectionto a set of values usingkeyMappermapping.static <T> java.util.Collection<T>emptyIfNull(java.util.Collection<T> collection)Safe wrapper around nullable collection instances: returnscollectionargument itself, if thecollectionis 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: returnslistargument itself, if thelistis 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: returnsmapargument itself, if themapis 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: returnssetargument itself, if thesetis 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 satisfyincludeConditionpredicate.
-
-
-
Method Detail
-
filterCollection
@Nonnull public static <T> java.util.stream.Stream<T> filterCollection(@Nullable java.util.Collection<T> collection, @Nonnull java.util.function.Predicate<T> includeCondition)Create a new collection which contains only elements which satisfyincludeConditionpredicate.- Type Parameters:
T- type of the collection items.- Parameters:
collection-Collectionto be filteredincludeCondition- condition which verifies whether the value should be included to the final result.- Returns:
- new filtered stream with items which satisfy
includeConditionpredicate. Ifcollectionis null or empty - empty stream is returned.
-
collectionToSet
@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)Convert acollectionto a set of values usingkeyMappermapping.If the collection has duplicate keys - only one will be stored, which one is not defined though.
- Type Parameters:
T- type of collection entriesK- type of Set key- Parameters:
collection-Collectionto convertkeyMapper- function which converts the collection entry to a key.- Returns:
- new
Setwhich consists of items, converted from <T> entries to keys usingentryToKeyfunction.
-
collectionToMap
@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)Convert acollectionto a map usingkeyMapperandvalueMappermappers. If keys are duplicated - only one value is stored, which one - undefined.- Type Parameters:
T- type of collection entriesK- type of Map keyV- type of Map value- Parameters:
collection-Collectionto convertkeyMapper- function which converts the collection entry to a keyvalueMapper- function which converts the collection entry to a value- Returns:
- new
Mapwhich consists of key-value pairs, converted from <T> entries - See Also:
collectionToMap(Collection, Function)
-
collectionToMap
@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)Same ascollectionToMap(Collection, Function, Function), but uses entries themselves as map values.- Type Parameters:
T- type of collection entriesK- type of Map key- Parameters:
collection-Collectionto convertkeyMapper- function which converts the collection entry to a key- Returns:
- new
Mapwhich consists of key-value pairs, converted from <T> entries - See Also:
collectionToMap(Collection, Function, Function)
-
emptyIfNull
@Nonnull public static <T> java.util.Collection<T> emptyIfNull(@Nullable java.util.Collection<T> collection)Safe wrapper around nullable collection instances: returnscollectionargument itself, if thecollectionis non-null, otherwise returns (immutable) empty collection.- Type Parameters:
T- collection entities type- Parameters:
collection-Collectioninstance to process- Returns:
- original
collectioninstance, if non-null; otherwise immutable empty collection instance. - See Also:
emptyIfNull(List),emptyIfNull(Set),emptyIfNull(Map)
-
emptyIfNull
@Nonnull public static <T> java.util.List<T> emptyIfNull(@Nullable java.util.List<T> list)Safe wrapper around nullable list instances: returnslistargument itself, if thelistis non-null, otherwise returns (immutable) empty list.- Type Parameters:
T- list entities type- Parameters:
list-Listinstance to process- Returns:
- original
listinstance, if non-null; otherwise immutable empty list instance. - See Also:
emptyIfNull(Collection),emptyIfNull(Set),emptyIfNull(Map)
-
emptyIfNull
@Nonnull public static <T> java.util.Set<T> emptyIfNull(@Nullable java.util.Set<T> set)Safe wrapper around nullable set instances: returnssetargument itself, if thesetis non-null, otherwise returns (immutable) empty set.- Type Parameters:
T- set entities type- Parameters:
set-Setinstance to process- Returns:
- original
setinstance, if non-null; otherwise immutable empty set instance. - See Also:
emptyIfNull(Collection),emptyIfNull(List),emptyIfNull(Map)
-
emptyIfNull
@Nonnull public static <K,V> java.util.Map<K,V> emptyIfNull(@Nullable java.util.Map<K,V> map)Safe wrapper around nullable map instances: returnsmapargument itself, if themapis non-null, otherwise returns (immutable) empty map.- Type Parameters:
K- map key typeV- map value type- Parameters:
map-Mapinstance to process- Returns:
- original
mapinstance, if non-null; otherwise immutable empty map instance. - See Also:
emptyIfNull(Collection),emptyIfNull(List),emptyIfNull(Set)
-
-