public class CollectionUtils extends java.lang.Object
Collections
.Modifier and Type | Class and Description |
---|---|
static interface |
CollectionUtils.BiConsumerWithException<S,T,E extends java.lang.Exception>
This interface is the same as
BiConsumer , except it allows throwing a
checked exceptions. |
static interface |
CollectionUtils.BiConsumerWithTwoExceptions<S,T,E1 extends java.lang.Exception,E2 extends java.lang.Exception>
This interface is the same as
BiConsumer , except it allows throwing
two checked exceptions. |
static interface |
CollectionUtils.BiFunctionWithException<T,U,R,E extends java.lang.Exception>
This interface is the same as
BiFunction , except it allows throwing a
checked exception. |
static interface |
CollectionUtils.ConsumerWithException<T,E extends java.lang.Exception>
This interface is the same as
Consumer , except it allows throwing a
checked exception. |
static interface |
CollectionUtils.ConsumerWithTwoExceptions<T,E1 extends java.lang.Exception,E2 extends java.lang.Exception>
This interface is the same as
Consumer , except it allows throwing two
checked exceptions. |
static interface |
CollectionUtils.FunctionWithException<T,R,E extends java.lang.Exception>
This interface is the same as
Function , except it allows throwing a
checked exception. |
static interface |
CollectionUtils.SupplierWithException<T,E extends java.lang.Exception>
This interface is the same as
Supplier , except it allows throwing a
checked exception. |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
MULTI_VALUE_DELIMITER
Delimiter used in strings to separate list values.
|
Constructor and Description |
---|
CollectionUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> void |
addAllSafe(java.util.Collection<T> collection1,
java.util.Collection<T> collection2)
Adds all elements of collection2 to collection1.
|
static <T> boolean |
allMatch(java.util.Collection<? extends T> collection,
java.util.function.Predicate<T> predicate)
Returns if all elements in the collection match the predicate.
|
static <T> boolean |
anyMatch(java.util.Collection<? extends T> collection,
java.util.function.Predicate<T> predicate)
Returns if any element in the collection matches the predicate.
|
static <T> java.util.HashSet<T> |
asHashSet(T... elements)
Create a hashed set from an array.
|
static <K,V> UnmodifiableMap<K,V> |
asMap(java.util.Map<K,V> baseMap,
Pair<K,V>... pairs)
Extends the given map with the pairs and returns an unmodifiable version.
|
static <K,V> UnmodifiableMap<K,V> |
asMap(Pair<K,V>... pairs)
Creates a new unmodifiable
HashMap based on given pairs. |
static <T> java.util.List<T> |
asRandomAccessList(java.util.Collection<T> list)
Returns a list implementation that allows for efficient random access.
|
static <T extends java.lang.Comparable<? super T>> |
asSortedUnmodifiableList(java.util.Collection<T> collection)
Returns a sorted unmodifiable list for a collection.
|
static <T> UnmodifiableCollection<T> |
asUnmodifiable(java.util.Collection<T> c)
Returns a new unmodifiable collection wrapping the given collection.
|
static <T> UnmodifiableList<T> |
asUnmodifiable(java.util.List<T> l)
Returns a new unmodifiable list wrapping the given list.
|
static <S,T> UnmodifiableMap<S,T> |
asUnmodifiable(java.util.Map<S,T> m)
Returns a new unmodifiable map wrapping the given map.
|
static <T> UnmodifiableSet<T> |
asUnmodifiable(java.util.Set<T> s)
Returns a new unmodifiable set wrapping the given set.
|
static <S,T> UnmodifiableSortedMap<S,T> |
asUnmodifiable(java.util.SortedMap<S,T> m)
Returns a new unmodifiable sorted map wrapping the given sorted map.
|
static <T> UnmodifiableSortedSet<T> |
asUnmodifiable(java.util.SortedSet<T> s)
Returns a new unmodifiable sorted set wrapping the given sorted set.
|
static <T> UnmodifiableSet<T> |
asUnmodifiableHashSet(T... elements)
Creates an unmodifiable hash set from an array.
|
static <T1,T2,RESULT> |
combine(java.util.Collection<T1> firstValues,
java.util.Collection<T2> secondValues,
java.util.function.BiFunction<T1,T2,RESULT> combineFunction)
Creates a
List that holds the combination of the values of two
collections. |
static <T1,T2,RESULT,COLLECTION extends java.util.Collection<RESULT>> |
combine(java.util.Collection<T1> firstValues,
java.util.Collection<T2> secondValues,
COLLECTION resultCollection,
java.util.function.BiFunction<T1,T2,RESULT> combineFunction)
Creates a
Collection that holds the combination of the values of two
collections. |
static <T> java.util.List<ImmutablePair<T,T>> |
computeUnorderedPairs(java.util.Collection<T> collection)
Compute list of unordered pairs for all elements contained in a collection.
|
static <T> T[] |
concatenateArrays(T[] a,
T[] b)
Concatenate two arrays to a new one.
|
static <T> T[] |
copyArray(T[] original)
Copy an array.
|
static <T> java.util.HashSet<T> |
differenceSet(java.util.Collection<T> collection1,
java.util.Collection<? extends T>... furtherCollections)
Returns the set-theoretic difference between the first and the additional
collections, i.e.
|
static <S> java.util.List<java.util.Set<S>> |
divideEvenlyIntoKSubsets(java.util.Set<S> set,
int k,
java.util.Random rnd)
Divides the given set randomly but, as far as possible, evenly into k subsets
of equal size, i.e., if set.size() is not a multiple of k, (k-(set.size()
modulo k)) of the resulting sets have one element less.
|
static <T> java.util.function.Consumer<T> |
emptyConsumer()
Returns an empty
Consumer that does nothing. |
static <T> java.util.List<T> |
emptyIfNull(java.util.List<T> input)
Returns the input list, or
emptyList() if the input is null. |
static <K,V,M extends java.util.Map<K,V>> |
emptyIfNull(M input,
java.util.function.Supplier<M> supplier)
Returns the input map, or uses the given supplier to create a new one.
|
static <T> UnmodifiableList<T> |
emptyList()
The way this method is defined allows to assign it to all parameterized
types, i.e.
|
static <S,T> UnmodifiableMap<S,T> |
emptyMap()
Returns an empty map.
|
static <T> UnmodifiableSet<T> |
emptySet()
Returns an empty set.
|
static <T extends java.lang.Enum<T>> |
enumUnionSet(java.util.EnumSet<T> initialSet,
java.util.EnumSet<T>... otherSets)
Return a set containing the union of all provided
EnumSet s. |
static <T> java.util.List<T> |
filter(java.util.Collection<T> collection,
java.util.function.Predicate<? super T> filter)
Filters the collection by testing all items against the
Predicate and
returning a List of those for which it returns true . |
static <T,R> java.util.List<R> |
filterAndMap(java.util.Collection<T> list,
java.util.function.Predicate<? super T> filter,
java.util.function.Function<? super T,? extends R> mapper)
Applies the mapper to all items in the list, for which the filter
Predicate returns true and returns the results as a
List . |
static <T> java.util.List<T> |
filterNullEntries(java.util.List<T> list)
Returns a new list containing only the non-null elements of the given list.
|
static <T> java.util.List<T> |
filterNullEntries(T[] array)
Returns a new list containing only the non-null elements of the given array.
|
static <T> java.util.Set<T> |
filterToSet(java.util.Collection<T> collection,
java.util.function.Predicate<? super T> filter)
Filters the collection by testing all items against the
Predicate and
returning a Set of those for which it returns true . |
static <T,E extends java.lang.Exception> |
filterWithException(java.util.Collection<T> collection,
CollectionUtils.FunctionWithException<? super T,java.lang.Boolean,E> filter)
Filters the collection by testing all items against the given predicate and
returning a
List of those for which it returns true . |
static <T1,T2> void |
forEach(java.util.Collection<T1> firstValues,
java.util.Collection<T2> secondValues,
java.util.function.BiConsumer<T1,T2> combineFunction)
Applies the function to the values of both collections at the same index.
|
static <T1,T2,E extends java.lang.Exception> |
forEachWithException(java.util.Collection<T1> firstValues,
java.util.Collection<T2> secondValues,
CollectionUtils.BiConsumerWithException<? super T1,? super T2,E> combineFunction)
Applies the function to the values of both collections at the same index.
|
static <T> java.util.List<java.util.List<T>> |
getAllPermutations(T... elements)
Obtain all permutations of the provided elements.
|
static <T> T |
getAny(java.lang.Iterable<T> iterable)
Returns one object from an
Iterable or null if the
iterable is empty. |
static <T> java.util.Set<T> |
getDuplicates(java.util.List<T> values)
Returns all values, which occur multiple times in the list.
|
static <T> java.util.ArrayList<T> |
getIndices(java.util.List<T> list,
java.util.List<java.lang.Integer> indices)
Returns a new List containing only the elements at the given indices.
|
static <T> T |
getLast(java.util.List<T> list)
Returns the last element in list or
null , if list is empty. |
static <L extends java.util.List<T>,T extends java.lang.Comparable<T>> |
getListComparator()
Returns a
Comparator that compares lists. |
static <L extends java.util.List<T>,T> |
getListComparator(java.util.Comparator<T> elementComparator)
Returns a
Comparator that compares lists. |
static java.util.List<java.lang.Integer> |
getNullIndices(java.util.List<?> list)
Returns the indices of null elements in the given list.
|
static <P extends Pair<T,S>,T extends java.lang.Comparable<T>,S extends java.lang.Comparable<S>> |
getPairComparator()
|
static <T> java.util.List<java.util.List<T>> |
getPowerSet(java.util.List<T> input)
Returns the power set of the given input list.
|
static <T> java.util.List<T> |
getRest(java.util.List<T> list)
Returns the sublist of all but the first element in list or
null , if list is empty. |
static <T> int |
indexOfFirstMatch(java.util.List<T> list,
java.util.function.Predicate<T> predicate)
Returns the element index of the first element in the list that matches the
given predicate.
|
static <T> java.util.HashSet<T> |
intersectionSet(java.util.Collection<T> collection1,
java.util.Collection<T>... furtherCollections)
Return a set containing the intersection of all provided collections.
|
static boolean |
isNullOrEmpty(java.util.Collection<?> collection)
Checks whether collection is null or empty
|
static boolean |
isNullOrEmpty(java.util.Map<?,?> map)
Checks whether map is null or empty
|
static <T,R> java.util.List<R> |
map(java.util.Collection<T> list,
java.util.function.Function<? super T,? extends R> mapper)
Applies the mapper
Function to all items in the collection and
returns the resulting List . |
static <K1,V1,K2,V2> |
map(java.util.Map<K1,V1> map,
java.util.function.Function<K1,? extends K2> keyMapper,
java.util.function.Function<V1,? extends V2> valueMapper)
Applies the key and value mapper
Function s to all keys/values in the
collection and returns the resulting Map . |
static <T,R> java.util.List<R> |
map(T[] array,
java.util.function.Function<? super T,? extends R> mapper)
Applies the mapper
Function to all items in the array and returns the
resulting List . |
static <T,R> java.util.List<R> |
mapDistinct(java.util.Collection<T> list,
java.util.function.Function<? super T,? extends R> mapper)
Applies the mapper
Function to all items in the collection, but only
adds the result item to the return list if it is not already in the list. |
static <T,R> java.util.Set<R> |
mapToSet(java.util.Collection<T> list,
java.util.function.Function<? super T,? extends R> mapper)
Applies the mapper
Function to all items in the collection and
returns the resulting Set only containing distinct mapped values. |
static <T,R,E extends java.lang.Exception> |
mapWithException(java.util.Collection<T> list,
CollectionUtils.FunctionWithException<? super T,? extends R,? extends E> mapper)
Applies the mapper
CollectionUtils.FunctionWithException to all items in the list and
returns the resulting List . |
static java.util.List<java.lang.String> |
parseMultiValueStringToList(java.util.List<java.lang.String> valueListLines)
Parses a
List of String representing lines of values to a
list of the single values. |
static java.util.List<java.lang.String> |
parseMultiValueStringToList(java.lang.String valueList)
Splits a
String representing a list of values to a list of lines and
forwards it to parseMultiValueStringToList(List) . |
static <T> java.util.List<T> |
remove(java.util.List<T> list,
int index)
Returns a list that contains all elements of the specified list except
the element at the specified index.
|
static <T> T[] |
removeElementFromArray(T element,
T[] array)
Removes an element from the array and returns the new array.
|
static <T> java.util.List<T> |
removeIndicesFrom(java.util.List<T> elements,
java.util.Set<java.lang.Integer> indices)
Returns a new List containing all elements of the given list except for those
with the given indices.
|
static <T> java.util.ArrayList<T> |
repeat(java.util.function.Function<java.lang.Integer,T> factory,
int count)
Returns an
ArrayList filled with the given count of objects created
by the factory function. |
static <T> java.util.ArrayList<T> |
reverse(java.util.Collection<T> list)
Returns a list that contains the elements of the specified list in reversed
order.
|
static <T extends java.lang.Comparable<? super T>> |
sort(java.util.Collection<T> collection)
Sorts the specified list into ascending order, according to the natural
ordering of its elements.
|
static <T> java.util.List<T> |
sort(java.util.Collection<T> collection,
java.util.Comparator<? super T> comparator)
Sorts the specified list according to the order induced by the specified
comparator.
|
static <S extends java.lang.Comparable<S>,T> |
sortByFirst(PairList<S,T> list)
Sorts the pair list by first index.
|
static <S,T> void |
sortByFirst(PairList<S,T> list,
java.util.Comparator<S> comparator)
Sorts the pair list with the given comparator.
|
static <T> java.util.List<T> |
subListFrom(java.util.List<T> list,
int fromIndex)
Wrapper for
List.subList(int, int) with only a from parameter. |
static <T> java.util.Set<T> |
subtract(java.util.Collection<T> collection,
java.util.Collection<T> elementsToRemove)
Creates a new set only containing those elements of the given collection that
are not in elementsToRemove.
|
static <T> T[] |
toArray(java.util.Collection<? extends T> collection,
java.lang.Class<T> type)
Convert collection to array.
|
static <T> Pair<java.util.List<T>,java.util.List<T>> |
topSort(java.util.Collection<T> collection,
java.util.function.Function<T,java.util.Collection<T>> getSuccessors)
Sorts the given collection topologically.
|
static <T> Pair<java.util.List<T>,java.util.Set<T>> |
topSortRemoveCycles(java.util.Collection<T> collection,
java.util.function.Function<T,java.util.Collection<T>> getSuccessors,
java.util.function.Function<T,java.util.Collection<T>> getPredecessors)
Sorts the given collection topologically, even if it contains cycles
(deterministically random elements in the cycle are removed).
|
static java.util.List<java.lang.String> |
toStringSet(java.util.Collection<?> input)
Maps all entries of the given Collection to their string representations.
|
static void |
truncateEnd(java.util.List<?> list,
int numElements)
Truncates the given list by removing elements from the end such that
numElements entries remain.
|
static <T> java.util.ArrayList<T> |
unionList(java.util.Collection<T> collection1,
java.util.Collection<T>... furtherCollections)
Return a set containing the union of all provided collections.
|
static <T> java.util.HashSet<T> |
unionSet(java.util.Collection<T> collection1,
java.util.Collection<T>... furtherCollections)
Return a set containing the union of all provided collections.
|
static <T> java.util.HashSet<T> |
unionSetAll(java.util.Collection<? extends java.util.Collection<T>> sets)
Return a set containing the union of all elements of the provided sets.
|
static <K,V> java.util.Map<K,V> |
zipAsMap(java.util.List<K> first,
java.util.List<V> second)
Returns a map that maps each element of the first list to the corresponding
(by index) element of the second list.
|
public static final java.lang.String MULTI_VALUE_DELIMITER
public CollectionUtils()
@SafeVarargs public static <T> java.util.HashSet<T> asHashSet(T... elements)
T
- typeelements
- elements in the set.Arrays.asList(Object[])
@SafeVarargs public static <K,V> UnmodifiableMap<K,V> asMap(Pair<K,V>... pairs)
HashMap
based on given pairs.@SafeVarargs public static <K,V> UnmodifiableMap<K,V> asMap(java.util.Map<K,V> baseMap, Pair<K,V>... pairs)
@SafeVarargs public static <T> UnmodifiableSet<T> asUnmodifiableHashSet(T... elements)
public static <T> UnmodifiableCollection<T> asUnmodifiable(java.util.Collection<T> c)
public static <T> UnmodifiableList<T> asUnmodifiable(java.util.List<T> l)
public static <S,T> UnmodifiableMap<S,T> asUnmodifiable(java.util.Map<S,T> m)
public static <T> UnmodifiableSet<T> asUnmodifiable(java.util.Set<T> s)
public static <S,T> UnmodifiableSortedMap<S,T> asUnmodifiable(java.util.SortedMap<S,T> m)
public static <T> UnmodifiableSortedSet<T> asUnmodifiable(java.util.SortedSet<T> s)
public static final <T> UnmodifiableList<T> emptyList()
UnmodifiableList<String> emptyList = CollectionUtils.emptyList(); UnmodifiableList<Date> emptyList = CollectionUtils.emptyList();
public static final <S,T> UnmodifiableMap<S,T> emptyMap()
emptyList()
for further details.public static final <T> UnmodifiableSet<T> emptySet()
emptyList()
for further details.public static <T extends java.lang.Comparable<? super T>> java.util.ArrayList<T> sort(java.util.Collection<T> collection)
All elements in the list must implement the Comparable interface.
Furthermore, all elements in the list must be mutually comparable (that is,
e1.compareTo(e2) must not throw a ClassCastException
for any
elements e1 and e2 in the list).
This method does not modify the original collection.
public static <T> java.util.ArrayList<T> reverse(java.util.Collection<T> list)
This method does not modify the original collection.
public static <T> java.util.Set<T> getDuplicates(java.util.List<T> values)
It doesn't tell you how often it occurs, just that it is more than once.
public static <T,R> java.util.List<R> map(java.util.Collection<T> list, java.util.function.Function<? super T,? extends R> mapper)
Function
to all items in the collection and
returns the resulting List
.
This method does not modify the original collection.
public static <T,R> java.util.Set<R> mapToSet(java.util.Collection<T> list, java.util.function.Function<? super T,? extends R> mapper)
Function
to all items in the collection and
returns the resulting Set
only containing distinct mapped values.
This method does not modify the original collection.
public static <K1,V1,K2,V2> java.util.Map<K2,V2> map(java.util.Map<K1,V1> map, java.util.function.Function<K1,? extends K2> keyMapper, java.util.function.Function<V1,? extends V2> valueMapper)
Function
s to all keys/values in the
collection and returns the resulting Map
.
This method does not modify the original collection.
public static <T,R> java.util.List<R> map(T[] array, java.util.function.Function<? super T,? extends R> mapper)
Function
to all items in the array and returns the
resulting List
.
This method does not modify the original array.
public static <T,R> java.util.List<R> mapDistinct(java.util.Collection<T> list, java.util.function.Function<? super T,? extends R> mapper)
Function
to all items in the collection, but only
adds the result item to the return list if it is not already in the list.
Returns the resulting List
.
This method does not modify the original collection.
public static <T,R,E extends java.lang.Exception> java.util.List<R> mapWithException(java.util.Collection<T> list, CollectionUtils.FunctionWithException<? super T,? extends R,? extends E> mapper) throws E extends java.lang.Exception
CollectionUtils.FunctionWithException
to all items in the list and
returns the resulting List
.
This method does not modify the original collection.
E
- if the mapper function throws this exception for any of the
elements of the original list.E extends java.lang.Exception
public static <T> java.util.ArrayList<T> getIndices(java.util.List<T> list, java.util.List<java.lang.Integer> indices)
public static <T> java.util.List<T> removeIndicesFrom(java.util.List<T> elements, java.util.Set<java.lang.Integer> indices)
public static java.util.List<java.lang.Integer> getNullIndices(java.util.List<?> list)
public static <T> java.util.List<T> filter(java.util.Collection<T> collection, java.util.function.Predicate<? super T> filter)
Predicate
and
returning a List
of those for which it returns true
.
This method does not modify the original collection.
public static <T,E extends java.lang.Exception> java.util.List<T> filterWithException(java.util.Collection<T> collection, CollectionUtils.FunctionWithException<? super T,java.lang.Boolean,E> filter) throws E extends java.lang.Exception
List
of those for which it returns true
.
Propagates checked exceptions of the given predicate.
This method does not modify the original collection.
E extends java.lang.Exception
public static <T> java.util.Set<T> filterToSet(java.util.Collection<T> collection, java.util.function.Predicate<? super T> filter)
Predicate
and
returning a Set
of those for which it returns true
.
This method does not modify the original collection.
public static <T,R> java.util.List<R> filterAndMap(java.util.Collection<T> list, java.util.function.Predicate<? super T> filter, java.util.function.Function<? super T,? extends R> mapper)
Predicate
returns true
and returns the results as a
List
.
This method does not modify the original collection.
public static <T> java.util.List<T> remove(java.util.List<T> list, int index)
This method does not modify the original list.
public static <T> java.util.List<T> sort(java.util.Collection<T> collection, java.util.Comparator<? super T> comparator)
All elements in the list must implement the Comparable interface.
Furthermore, all elements in the list must be mutually comparable (that is,
e1.compareTo(e2) must not throw a ClassCastException
for any
elements e1 and e2 in the list).
This method does not modify the original collection.
public static <T extends java.lang.Comparable<? super T>> UnmodifiableList<T> asSortedUnmodifiableList(java.util.Collection<T> collection)
public static <T> T getAny(java.lang.Iterable<T> iterable)
Iterable
or null
if the
iterable is empty.public static <T> T[] toArray(java.util.Collection<? extends T> collection, java.lang.Class<T> type)
Collection.toArray(Object[])
.public static <T> T[] copyArray(T[] original)
Arrays.copyOf(Object[], int)
that does not require to specify the length.public static <T> java.util.List<ImmutablePair<T,T>> computeUnorderedPairs(java.util.Collection<T> collection)
public static <T> T getLast(java.util.List<T> list)
null
, if list is empty.public static <T> java.util.List<T> getRest(java.util.List<T> list)
null
, if list is empty.public static <S extends java.lang.Comparable<S>,T> void sortByFirst(PairList<S,T> list)
public static <S,T> void sortByFirst(PairList<S,T> list, java.util.Comparator<S> comparator)
public static <T> java.util.List<T> asRandomAccessList(java.util.Collection<T> list)
@SafeVarargs public static <T> java.util.HashSet<T> unionSet(java.util.Collection<T> collection1, java.util.Collection<T>... furtherCollections)
HashSet
, i.e. the elements should support hashing.
We use two separate arguments to ensure on the interface level that at least
one collection is provided. This is transparent for the caller.
All arguments can be null. The result will always be non-null and will be an
empty set if all arguments are null.@SafeVarargs public static <T extends java.lang.Enum<T>> java.util.EnumSet<T> enumUnionSet(java.util.EnumSet<T> initialSet, java.util.EnumSet<T>... otherSets)
EnumSet
s.
We use two separate arguments to ensure on the interface level that at least
one collection is provided. This is transparent for the caller.
None of the arguments may be null.@SafeVarargs public static <T> java.util.ArrayList<T> unionList(java.util.Collection<T> collection1, java.util.Collection<T>... furtherCollections)
ArrayList
and the result preserves duplicates between and within the
collections.
We use two separate arguments to ensure on the interface level that at least
one collection is provided. This is transparent for the caller.
All arguments can be null. The result will always be non-null and will be an
empty set if all arguments are null.public static <T> java.util.HashSet<T> unionSetAll(java.util.Collection<? extends java.util.Collection<T>> sets)
HashSet
, i.e. the elements should support hashing.public static <T> java.util.Set<T> subtract(java.util.Collection<T> collection, java.util.Collection<T> elementsToRemove)
null
.public static <T> void addAllSafe(java.util.Collection<T> collection1, java.util.Collection<T> collection2)
null
, in which case nothing happens.@SafeVarargs public static <T> java.util.HashSet<T> intersectionSet(java.util.Collection<T> collection1, java.util.Collection<T>... furtherCollections)
HashSet
, i.e. the elements should support hashing.
We use two separate arguments to ensure on the interface level that at least
one collection is provided. This is transparent for the caller.@SafeVarargs public static <T> java.util.HashSet<T> differenceSet(java.util.Collection<T> collection1, java.util.Collection<? extends T>... furtherCollections)
HashSet
, so the
elements should support hashing.public static boolean isNullOrEmpty(java.util.Collection<?> collection)
public static boolean isNullOrEmpty(java.util.Map<?,?> map)
public static void truncateEnd(java.util.List<?> list, int numElements)
public static <S> java.util.List<java.util.Set<S>> divideEvenlyIntoKSubsets(java.util.Set<S> set, int k, java.util.Random rnd)
S
- must support hashing.public static <T> java.util.List<java.util.List<T>> getAllPermutations(T... elements)
public static <T> java.util.List<java.util.List<T>> getPowerSet(java.util.List<T> input)
public static <T> java.util.List<T> emptyIfNull(java.util.List<T> input)
emptyList()
if the input is null.public static <K,V,M extends java.util.Map<K,V>> M emptyIfNull(M input, java.util.function.Supplier<M> supplier)
public static <T> T[] removeElementFromArray(T element, T[] array)
public static <T> java.util.List<T> filterNullEntries(java.util.List<T> list)
public static <T> java.util.List<T> filterNullEntries(T[] array)
public static <T> T[] concatenateArrays(T[] a, T[] b)
public static <T> boolean anyMatch(java.util.Collection<? extends T> collection, java.util.function.Predicate<T> predicate)
public static <T> boolean allMatch(java.util.Collection<? extends T> collection, java.util.function.Predicate<T> predicate)
public static <T> Pair<java.util.List<T>,java.util.List<T>> topSort(java.util.Collection<T> collection, java.util.function.Function<T,java.util.Collection<T>> getSuccessors)
getSuccessors
function must establish an order on the elements
of the collection (more details below).
The sorting is stable if the stream() iterator on the given collection is
stable.
The result of this function is a Pair
. The first item of the
pair contains the sorted collection as a list. The second item contains a set
of elements that could not be fit into the topological order (in case there
are cycles in the partial order). This second item is empty if there are no
cycles. Elements of the second item are not contained in the sorted list, so
callers should check whether the second item is empty.
Direction of the sorting: if getSuccessors(A).contains(B)
, then
A
will be before B
in the sorted list.
Details on the order established by getSuccessors
: In
literature, this should be a partial order, but we don't seem to require the
properties of partial orders (reflexivity, transitivity, antisymmetry). We
just need an order relation that maps each element to its direct successors.
In fact this method will be faster when the order relation is smaller, so
forget reflexivity and transitivity. Antisymmetry would be nice (otherwise
you'll get a cycle for sure).
If getSuccessors
returns null for an element, we will treat this
as if it was an empty collection.collection
- the collection to be sortedgetSuccessors
- the order relation (described above)public static <T> Pair<java.util.List<T>,java.util.Set<T>> topSortRemoveCycles(java.util.Collection<T> collection, java.util.function.Function<T,java.util.Collection<T>> getSuccessors, java.util.function.Function<T,java.util.Collection<T>> getPredecessors)
getSuccessors
function must establish an order on the elements
of the collection (more details below). The given
getPredecessors
function must establish the reverse order of
getSuccessors
.
The sorting is stable if the stream() iterator on the given collection is
stable.
The result of this function is a Pair
. The first item of the
pair contains the sorted collection as a list. The second item contains a
list of cycle elements that were removed to enable topological sorting.
Direction of the sorting: if getSuccessors(A).contains(B)
, then
A
will be before B
in the sorted list.
For detail requirements on getSuccessors
and
getPredecessors
, see topSort(Collection, Function)
.collection
- the collection to be sortedgetSuccessors
- the order relation (described above)public static java.util.List<java.lang.String> toStringSet(java.util.Collection<?> input)
public static <L extends java.util.List<T>,T extends java.lang.Comparable<T>> java.util.Comparator<L> getListComparator()
Comparator
that compares lists. Shorter lists are
considered "smaller" than longer lists. If lists have the same length,
elements are compared using their compareTo methods until one is found that
is does not return 0 when compared to its counterpart. If list lengths are
equal and all elements return 0 on comparison, the returned
Comparator
returns 0.public static <L extends java.util.List<T>,T> java.util.Comparator<L> getListComparator(java.util.Comparator<T> elementComparator)
Comparator
that compares lists. Shorter lists are
considered "smaller" than longer lists. If lists have the same length,
elements are compared using the given elementComparator until one is found
that is does not return 0 when compared to its counterpart. If list lengths
are equal and all elements return 0 on comparison, the returned
Comparator
returns 0.public static <P extends Pair<T,S>,T extends java.lang.Comparable<T>,S extends java.lang.Comparable<S>> java.util.Comparator<P> getPairComparator()
Comparator
that compares Pair
s of
Comparable
s. First compares the first elements and if their
comparison returns 0, compares the second elements. If their comparison also
return 0, this Comparator
returns 0.public static <T> java.util.function.Consumer<T> emptyConsumer()
Consumer
that does nothing.public static java.util.List<java.lang.String> parseMultiValueStringToList(java.lang.String valueList)
String
representing a list of values to a list of lines and
forwards it to parseMultiValueStringToList(List)
.public static java.util.List<java.lang.String> parseMultiValueStringToList(java.util.List<java.lang.String> valueListLines)
List
of String
representing lines of values to a
list of the single values. The value entries must be separated by
",".public static <T1,T2,RESULT> java.util.List<RESULT> combine(java.util.Collection<T1> firstValues, java.util.Collection<T2> secondValues, java.util.function.BiFunction<T1,T2,RESULT> combineFunction)
List
that holds the combination of the values of two
collections.public static <T1,T2,RESULT,COLLECTION extends java.util.Collection<RESULT>> COLLECTION combine(java.util.Collection<T1> firstValues, java.util.Collection<T2> secondValues, COLLECTION resultCollection, java.util.function.BiFunction<T1,T2,RESULT> combineFunction)
Collection
that holds the combination of the values of two
collections.
Both collections must be of the same size. The order of insertion into the
new Collection
is determined by the order imposed by the collections'
iterators.public static <T1,T2> void forEach(java.util.Collection<T1> firstValues, java.util.Collection<T2> secondValues, java.util.function.BiConsumer<T1,T2> combineFunction)
public static <T1,T2,E extends java.lang.Exception> void forEachWithException(java.util.Collection<T1> firstValues, java.util.Collection<T2> secondValues, CollectionUtils.BiConsumerWithException<? super T1,? super T2,E> combineFunction) throws E extends java.lang.Exception
E extends java.lang.Exception
public static <T> int indexOfFirstMatch(java.util.List<T> list, java.util.function.Predicate<T> predicate)
public static <K,V> java.util.Map<K,V> zipAsMap(java.util.List<K> first, java.util.List<V> second)
public static <T> java.util.List<T> subListFrom(java.util.List<T> list, int fromIndex)
List.subList(int, int)
with only a from parameter.
Returns a view of the portion of this list between the specified
fromIndex, inclusive, and the end of the list. The returned list is
backed by this list, so non-structural changes in the returned list are
reflected in this list, and vice-versa.fromIndex
- low endpoint (inclusive) of the subListjava.lang.IndexOutOfBoundsException
- for an illegal endpoint index value
(fromIndex < 0 || fromIndex > size)public static <T> java.util.ArrayList<T> repeat(java.util.function.Function<java.lang.Integer,T> factory, int count)
ArrayList
filled with the given count of objects created
by the factory function. The current count is passed to the factory funciton.