public abstract class CollectionUtils extends Object
Modifier and Type | Field and Description |
---|---|
static Function<Object,String> |
TO_STRING_TRANSFORMER |
Constructor and Description |
---|
CollectionUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> Set<T> |
asSet(Class<T> clazz,
Object... objects)
This utility method converts a vararg of Objects into a Set
|
static <T> Set<T> |
asSet(T... objects)
This utility method converts a vararg of Objects into a Set
|
static <T> Filter<T> |
containsFilter(Collection<T> values) |
static <T> List<T> |
filter(Collection<T> values,
Function<? super T,Boolean> filter)
Returns a filtered
List of values. |
static <K,V> Map<K,V> |
filterKeys(Map<K,V> map,
Function<? super K,? extends Boolean> filter)
This method can be used to filter a Map.
|
static <T> T |
findFirst(Collection<T> values,
Function<? super T,Boolean> acceptor)
Finds the first value for which
acceptor returns true . |
static <T> List<T> |
flatten(Collection<? extends Collection<? extends T>> values)
This method flattens the provided collection of collections of values into a single
List object containing each of the elements from the provided sub-collections. |
static <T> List<T> |
flatten(Collection<? extends T>... collections)
|
static <T> List<T> |
intersect(List<? extends T> list1,
List<? extends T> list2)
This method returns a new ArrayList which is the intersection of the two List parameters, based on
equality
of their elements. |
static <K,V> Map<K,V> |
intersect(Map<K,V> map1,
Map<K,V> map2)
This method returns a new HashMap which is the intersection of the two Map parameters, based on
equality
of their entries. |
static <T> Set<T> |
intersect(Set<? extends T> set1,
Set<? extends T> set2)
This method returns a new HashSet which is the intersection of the two Set parameters, based on
equality
of their elements. |
static boolean |
isEmpty(Collection<?> items) |
static boolean |
isEmpty(Map<?,?> map) |
static <T> List<T> |
moveLeft(int offset,
T element,
List<T> list)
This method returns a new List instance containing the same element objects as the provided
list, but with the specified element having been moved left by the specified offset.
|
static <T> List<T> |
moveRight(int offset,
T element,
List<T> list)
This method does the same as
moveLeft(int, Object, List) but it moves the specified element
to the right instead of the left. |
static <T> List<T> |
nullSafeAppend(List<T> first,
List<T> second)
This method joins two lists returning the a single list consisting of the first followed by the second.
|
static <T> List<T> |
nullSafeAppend(List<T> first,
List<T> second,
boolean emptyResultIsNull)
This method joins two lists returning the a single list consisting of the first followed by the second.
|
static <K,V> Map<K,V> |
nullSafeMerge(Map<K,V> first,
Map<K,V> second)
This method merges two maps returning the union of both maps.
|
static <K,V> Map<K,V> |
nullSafeMerge(Map<K,V> first,
Map<K,V> second,
boolean emptyResultIsNull)
This method merges two maps returning the union of both maps.
|
static <T> Set<T> |
nullSafeMerge(Set<T> first,
Set<T> second)
This method merges two sets returning the union of both sets.
|
static <T> Set<T> |
nullSafeMerge(Set<T> first,
Set<T> second,
boolean emptyResultIsNull)
This method merges two sets returning the union of both sets.
|
static <K,V> Map<K,V> |
sortMapByValue(Map<K,V> map,
Comparator<Map.Entry<K,V>> valueComparator)
Creates a new sorted map, based on the values from the given map and Comparator.
|
static <K,V> Comparator<Map.Entry<K,V>> |
toEntryComparator(Comparator<V> valueComparator)
This method offers convenient conversion from value-based comparators to entry-based comparators
for use with
sortMapByValue(Map, Comparator) above. |
static List<String> |
toListOfStrings(Collection<?> values) |
static <F,T> List<T> |
transform(Collection<F> values,
Function<? super F,? extends T> transformer)
|
static <F,T> List<T> |
transform(Function<? super F,? extends T> transformer,
F... values)
|
static <FK,FV,TK,TV> |
transform(Map<FK,FV> map,
Function<Map.Entry<FK,FV>,Pair<TK,TV>> transformer) |
static <F,T> List<T> |
transformFlat(Collection<F> values,
Function<? super F,? extends Collection<? extends T>> transformer) |
static <F,T,V> Map<T,V> |
transformKeys(Map<F,V> map,
Function<? super F,? extends T> transformer)
|
static <F,T> Map<F,T> |
transformToMap(Collection<F> values,
Function<F,T> transformer) |
static <T> Set<T> |
unmodifiableSet(Collection<T> values)
Returns an immutable Serializable Set containing the values.
|
static <T> Set<T> |
unmodifiableSet(T... values)
Returns an immutable Serializable Set containing the values.
|
public static boolean isEmpty(Map<?,?> map)
public static boolean isEmpty(Collection<?> items)
public static <T> Set<T> nullSafeMerge(Set<T> first, Set<T> second)
first
- first set. can be null.second
- second set. can be null.public static <T> Set<T> nullSafeMerge(Set<T> first, Set<T> second, boolean emptyResultIsNull)
first
- first set. can be null.second
- second set. can be null.emptyResultIsNull
- if the result is empty, should we return null?public static <K,V> Map<K,V> nullSafeMerge(Map<K,V> first, Map<K,V> second)
first
- first map. can be null.second
- second map. can be null.public static <K,V> Map<K,V> nullSafeMerge(Map<K,V> first, Map<K,V> second, boolean emptyResultIsNull)
first
- first map. can be null.second
- second map. can be null.emptyResultIsNull
- if the result is empty, should we return null?public static <T> List<T> nullSafeAppend(List<T> first, List<T> second)
first
- first list. can be null.second
- second list. can be null.public static <T> List<T> nullSafeAppend(List<T> first, List<T> second, boolean emptyResultIsNull)
first
- first list. can be null.second
- second list. can be null.emptyResultIsNull
- if the result is empty, should we return null?public static <F,T> List<T> transform(Collection<F> values, Function<? super F,? extends T> transformer)
Collection
of values of type F to a Serializable
List
of values of type T.
Filters out all values converted to null
.F
- From typeT
- To typevalues
- the values to convert.transformer
- Used to convert values.public static <F,T,V> Map<T,V> transformKeys(Map<F,V> map, Function<? super F,? extends T> transformer)
Map
having keys of type F to a new Map
instance having keys of type T. The object references
in the value set are copied to the transformed map, thus reusing the same objects.F
- From typeT
- To typeV
- The value type of the before and after maps.map
- the map to convert.transformer
- Used to convert keys.public static <F,T> List<T> transform(Function<? super F,? extends T> transformer, F... values)
Collection
of values of type F to a Serializable
List
of values of type T.
Filters out all values converted to null
.F
- From typeT
- To typevalues
- the values to convert.transformer
- Used to convert values.public static List<String> toListOfStrings(Collection<?> values)
public static <T> Set<T> asSet(T... objects)
objects
- the objects to be added to the setClassCastException
- if any of the supplied objects are not of type T.public static <T> Set<T> asSet(Class<T> clazz, Object... objects)
clazz
- the Set type to return.objects
- the objects to be added to the setClassCastException
- if any of the supplied objects are not of type T.public static <T> List<T> filter(Collection<T> values, Function<? super T,Boolean> filter)
List
of values. Only values for which filter.apply(T) returns true
are included in the List
or returned values.T
- The type of the Collection
values
- the Collection
to be filtered.filter
- the Function
used to filter the Collection
.List
of values.public static <T> List<T> flatten(Collection<? extends Collection<? extends T>> values)
List
object containing each of the elements from the provided sub-collections.
For example, flatten( [1, 2], [3], [], [4, 5, 6] )
would produce a List like [1, 2, 3, 4, 5, 6]
.
Here, "[]" represents any Java collection.T
- the element type of the collections. Note that this must be the same for all collections.values
- a collection of collections of elements to be flattened.@SafeVarargs public static <T> List<T> flatten(Collection<? extends T>... collections)
collections
- a vararg of Collection objects to be flattened into a list.public static <F,T> List<T> transformFlat(Collection<F> values, Function<? super F,? extends Collection<? extends T>> transformer)
public static <T> T findFirst(Collection<T> values, Function<? super T,Boolean> acceptor)
acceptor
returns true
.T
- Tvalues
- Collectionacceptor
- Function super T, Boolean>null
.public static <T> Set<T> unmodifiableSet(T... values)
T
- Tvalues
- T...public static <T> Set<T> unmodifiableSet(Collection<T> values)
T
- Tvalues
- Collectionpublic static <F,T> Map<F,T> transformToMap(Collection<F> values, Function<F,T> transformer)
values
- Collectiontransformer
- Functionpublic static <K,V> Map<K,V> filterKeys(Map<K,V> map, Function<? super K,? extends Boolean> filter)
filter function
returns true
, will be included in the resultant Map, else they will not.map
- the map whose entries are to be filtered.filter
- the filter function which is applied to the key.public static <FK,FV,TK,TV> Map<TK,TV> transform(Map<FK,FV> map, Function<Map.Entry<FK,FV>,Pair<TK,TV>> transformer)
public static <T> Filter<T> containsFilter(Collection<T> values)
public static <T> List<T> intersect(List<? extends T> list1, List<? extends T> list2)
equality
of their elements.
The intersection list will contain elements in the order they have in list1 and any references in the resultant list will be
to elements within list1 also.public static <K,V> Map<K,V> intersect(Map<K,V> map1, Map<K,V> map2)
equality
of their entries.
Any references in the resultant map will be to elements within map1.public static <T> Set<T> intersect(Set<? extends T> set1, Set<? extends T> set2)
equality
of their elements.
Any references in the resultant set will be to elements within set1.public static <K,V> Map<K,V> sortMapByValue(Map<K,V> map, Comparator<Map.Entry<K,V>> valueComparator)
map
- the map which needs to be sortedvalueComparator
- the Comparatorpublic static <K,V> Comparator<Map.Entry<K,V>> toEntryComparator(Comparator<V> valueComparator)
sortMapByValue(Map, Comparator)
above.
Call it like so: CollectionUtils.<String, Integer>toEntryComparator(valueComparator);
valueComparator
- a comparator which compares the value types from a Map.public static <T> List<T> moveLeft(int offset, T element, List<T> list)
offset
- the number of places over which to move the specified element.element
- the element to be moved.list
- the list to be reordered.NoSuchElementException
- if the list does not contain an element equal to the one specified.public static <T> List<T> moveRight(int offset, T element, List<T> list)
moveLeft(int, Object, List)
but it moves the specified element
to the right instead of the left.Copyright © 2005–2018 Alfresco Software. All rights reserved.