public class CollectionUtils extends Object
Provides utility methods related to dealing with Collections.
Modifier and Type | Method and Description |
---|---|
static <T,U> com.google.common.collect.ImmutableCollection<U> |
filterAndTransform(Collection<T> elements,
com.google.common.base.Function<T,Optional<U>> function)
Filters and transforms the matching elements.
|
static <T> com.google.common.collect.ImmutableList<T> |
remove(List<? extends T> source,
List<? extends T> toRemove)
Removes the given elements from the given list.
|
static <T> com.google.common.collect.ImmutableSet<T> |
remove(Set<? extends T> source,
Set<? extends T> toRemove)
Removes the given elements from the given set.
|
static <T> Pair<com.google.common.collect.ImmutableCollection<T>,com.google.common.collect.ImmutableCollection<T>> |
separate(Collection<T> elements,
com.google.common.base.Predicate<? super T> predicate)
Separates the elements of the given collection by the given predicate into two collections.
|
static <T> com.google.common.collect.ImmutableList<T> |
tailSubList(Collection<T> collection,
int n)
Returns the n last elements of the given collection.
|
static <E> com.google.common.collect.ImmutableSet<E> |
toSetKeepFirst(Collection<E> elements,
com.google.common.base.Equivalence<E> equivalence)
Returns a set of the given elements where element equality is defined by the given Equivalence instance.
|
static <E> com.google.common.collect.ImmutableSet<E> |
toSetKeepLast(Collection<E> elements,
com.google.common.base.Equivalence<E> equivalence)
Returns a set of the given elements where element equality is defined by the given Equivalence instance.
|
static <T> List<T> |
toTypedList(List list)
'Converts' the given list to a typed list.
|
static <K,V> Map<K,V> |
toTypedMap(Map map)
'Converts' the given map to a typed map.
|
static <T> Set<T> |
toTypedSet(Set set)
'Converts' the given set to a typed set.
|
static <V,W> List<Object> |
transformListRecursively(List<? extends Object> list,
com.google.common.base.Function<V,W> atomicValueFunction)
Recursively transforms the values of the given list by applying the given function.
|
static <K,V,W> Map<K,Object> |
transformMapRecursively(Map<K,? extends Object> map,
com.google.common.base.Function<V,W> atomicValueFunction)
Recursively transforms the values of the given map by applying the given function.
|
static <V,W> Set<Object> |
transformSetRecursively(Set<? extends Object> set,
com.google.common.base.Function<V,W> atomicValueFunction)
Recursively transforms the values of the given set by applying the given function.
|
static <T,V> Map<T,V> |
unmodifiableDetachedMap(Map<T,V> map)
Converts the map to an immutable map.
|
public static <V,W> List<Object> transformListRecursively(List<? extends Object> list, com.google.common.base.Function<V,W> atomicValueFunction)
Recursively transforms the values of the given list by applying the given function. Recursion occurs if and only if a list value is of type List
, Set
, or
Map
, all other value types are considered 'atomic' and will have the given transformation function applied.
V
- the type of the atomic values contained in this list, top-level or nestedW
- the type of the result valueslist
- the list whose values to transformatomicValueFunction
- the transformation function to apply on the list valuespublic static <V,W> Set<Object> transformSetRecursively(Set<? extends Object> set, com.google.common.base.Function<V,W> atomicValueFunction)
Recursively transforms the values of the given set by applying the given function. Recursion occurs if and only if a set value is of type List
, Set
, or
Map
, all other value types are considered 'atomic' and will have the given transformation function applied.
V
- the type of the atomic values contained in this set, top-level or nestedW
- the type of the result valuesset
- the set whose values to transformatomicValueFunction
- the transformation function to apply on the set valuespublic static <K,V,W> Map<K,Object> transformMapRecursively(Map<K,? extends Object> map, com.google.common.base.Function<V,W> atomicValueFunction)
Recursively transforms the values of the given map by applying the given function. Recursion occurs if and only if a map value is of type List
, Set
, or
Map
, all other value types are considered 'atomic' and will have the given transformation function applied.
K
- the type of the keyV
- the type of the atomic values contained in this map, top-level or nestedW
- the type of the result valuesmap
- the map whose values to transformatomicValueFunction
- the transformation function to apply on the map valuespublic static <T> com.google.common.collect.ImmutableList<T> tailSubList(Collection<T> collection, int n)
Returns the n last elements of the given collection. The order of the elements returned is the same as in the original collection.
T
- the type of the listcollection
- the collection from which to return the n last elementsn
- the number of elements to returnpublic static <E> com.google.common.collect.ImmutableSet<E> toSetKeepFirst(Collection<E> elements, com.google.common.base.Equivalence<E> equivalence)
Returns a set of the given elements where element equality is defined by the given Equivalence instance. In case of equality of n elements, the first element of the n elements is added to the Set.
E
- the type of the setelements
- the elements to put in a Setequivalence
- the Equivalence that defines equality of two elementspublic static <E> com.google.common.collect.ImmutableSet<E> toSetKeepLast(Collection<E> elements, com.google.common.base.Equivalence<E> equivalence)
Returns a set of the given elements where element equality is defined by the given Equivalence instance. In case of equality of n elements, the last element of the n elements is added to the Set.
E
- the type of the setelements
- the elements to put in a Setequivalence
- the Equivalence that defines equality of two elementspublic static <T> Pair<com.google.common.collect.ImmutableCollection<T>,com.google.common.collect.ImmutableCollection<T>> separate(Collection<T> elements, com.google.common.base.Predicate<? super T> predicate)
Separates the elements of the given collection by the given predicate into two collections.
T
- the type of the collectionelements
- the elements to separatepredicate
- the predicate by which to separate the elementspublic static <T,U> com.google.common.collect.ImmutableCollection<U> filterAndTransform(Collection<T> elements, com.google.common.base.Function<T,Optional<U>> function)
Filters and transforms the matching elements.
T
- the type of the elementsU
- the type of the returned and transformed collectionelements
- the elements to filter and transformfunction
- the transformation to apply, returns Optional#absent()
for non-matching elementspublic static <T,V> Map<T,V> unmodifiableDetachedMap(Map<T,V> map)
Converts the map to an immutable map. The map must not contain null values.
T
- the type of the keyV
- the type of the valuemap
- the map, can be nullpublic static <T> com.google.common.collect.ImmutableList<T> remove(List<? extends T> source, List<? extends T> toRemove)
Removes the given elements from the given list.
T
- the type of the listsource
- the list to remove fromtoRemove
- the elements to removepublic static <T> com.google.common.collect.ImmutableSet<T> remove(Set<? extends T> source, Set<? extends T> toRemove)
Removes the given elements from the given set.
T
- the type of the setsource
- the set to remove fromtoRemove
- the elements to removepublic static <T> List<T> toTypedList(List list)
'Converts' the given list to a typed list. Note that this is an unsafe operation since the compiler is tricked into thinking that the given list contains elements of the types given by the list which will store the result of calling this method. This method is only offered to avoid compiler warnings in the calling code.
T
- the type of the listlist
- the list to 'convert'public static <T> Set<T> toTypedSet(Set set)
'Converts' the given set to a typed set. Note that this is an unsafe operation since the compiler is tricked into thinking that the given set contains elements of the types given by the set which will store the result of calling this method. This method is only offered to avoid compiler warnings in the calling code.
T
- the type of the setset
- the set to 'convert'public static <K,V> Map<K,V> toTypedMap(Map map)
'Converts' the given map to a typed map. Note that this is an unsafe operation since the compiler is tricked into thinking that the given map contains keys and values of the types given by the map which will store the result of calling this method. This method is only offered to avoid compiler warnings in the calling code.
K
- the type of the keyV
- the type of the valuemap
- the map to 'convert'Copyright © 2010 - 2019 edorasware ag. All Rights Reserved.