public class CollectionUtils
extends java.lang.Object
Collection
instances.
Various utility methods might put the input objects into a Set/Map/Bag. In case
the input objects override Object.equals(Object)
, it is mandatory that
the general contract of the Object.hashCode()
method is maintained.
NOTE: From 4.0, method parameters will take Iterable
objects when possible.
Modifier and Type | Field and Description |
---|---|
static java.util.Collection |
EMPTY_COLLECTION
An empty unmodifiable collection.
|
Modifier and Type | Method and Description |
---|---|
static <C> boolean |
addAll(java.util.Collection<C> collection,
C[] elements)
Adds all elements in the array to the given collection.
|
static <C> boolean |
addAll(java.util.Collection<C> collection,
java.util.Enumeration<? extends C> enumeration)
Adds all elements in the enumeration to the given collection.
|
static <C> boolean |
addAll(java.util.Collection<C> collection,
java.lang.Iterable<? extends C> iterable)
Adds all elements in the
Iterable to the given collection. |
static <C> boolean |
addAll(java.util.Collection<C> collection,
java.util.Iterator<? extends C> iterator)
Adds all elements in the iteration to the given collection.
|
static <T> boolean |
addIgnoreNull(java.util.Collection<T> collection,
T object)
Adds an element to the collection unless the element is null.
|
static <O> int |
cardinality(O obj,
java.lang.Iterable<? super O> coll)
Deprecated.
since 4.1, use
IterableUtils.frequency(Iterable, Object) instead.
Be aware that the order of parameters has changed. |
static <O extends java.lang.Comparable<? super O>> |
collate(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b)
Merges two sorted Collections, a and b, into a single, sorted List
such that the natural ordering of the elements is retained.
|
static <O extends java.lang.Comparable<? super O>> |
collate(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b,
boolean includeDuplicates)
Merges two sorted Collections, a and b, into a single, sorted List
such that the natural ordering of the elements is retained.
|
static <O> java.util.List<O> |
collate(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b,
java.util.Comparator<? super O> c)
Merges two sorted Collections, a and b, into a single, sorted List
such that the ordering of the elements according to Comparator c is retained.
|
static <O> java.util.List<O> |
collate(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b,
java.util.Comparator<? super O> c,
boolean includeDuplicates)
Merges two sorted Collections, a and b, into a single, sorted List
such that the ordering of the elements according to Comparator c is retained.
|
static <I,O,R extends java.util.Collection<? super O>> |
collect(java.lang.Iterable<? extends I> inputCollection,
Transformer<? super I,? extends O> transformer,
R outputCollection)
Transforms all elements from input collection with the given transformer
and adds them to the output collection.
|
static <I,O> java.util.Collection<O> |
collect(java.lang.Iterable<I> inputCollection,
Transformer<? super I,? extends O> transformer)
Returns a new Collection containing all elements of the input collection
transformed by the given transformer.
|
static <I,O,R extends java.util.Collection<? super O>> |
collect(java.util.Iterator<? extends I> inputIterator,
Transformer<? super I,? extends O> transformer,
R outputCollection)
Transforms all elements from the input iterator with the given transformer
and adds them to the output collection.
|
static <I,O> java.util.Collection<O> |
collect(java.util.Iterator<I> inputIterator,
Transformer<? super I,? extends O> transformer)
Transforms all elements from the input iterator with the given transformer
and adds them to the output collection.
|
static boolean |
containsAll(java.util.Collection<?> coll1,
java.util.Collection<?> coll2)
Returns
true iff all elements of coll2 are also contained
in coll1 . |
static boolean |
containsAny(java.util.Collection<?> coll1,
java.util.Collection<?> coll2)
Returns
true iff at least one element is in both collections. |
static <C> int |
countMatches(java.lang.Iterable<C> input,
Predicate<? super C> predicate)
Deprecated.
since 4.1, use
IterableUtils.countMatches(Iterable, Predicate) instead |
static <O> java.util.Collection<O> |
disjunction(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b)
Returns a
Collection containing the exclusive disjunction
(symmetric difference) of the given Iterable s. |
static <T> java.util.Collection<T> |
emptyCollection()
Returns the immutable EMPTY_COLLECTION with generic type safety.
|
static <T> java.util.Collection<T> |
emptyIfNull(java.util.Collection<T> collection)
Returns an immutable empty collection if the argument is
null ,
or the argument itself otherwise. |
static <C> boolean |
exists(java.lang.Iterable<C> input,
Predicate<? super C> predicate)
Deprecated.
since 4.1, use
IterableUtils.matchesAny(Iterable, Predicate) instead |
static <E> E |
extractSingleton(java.util.Collection<E> collection)
Extract the lone element of the specified Collection.
|
static <T> boolean |
filter(java.lang.Iterable<T> collection,
Predicate<? super T> predicate)
Filter the collection by applying a Predicate to each element.
|
static <T> boolean |
filterInverse(java.lang.Iterable<T> collection,
Predicate<? super T> predicate)
Filter the collection by applying a Predicate to each element.
|
static <T> T |
find(java.lang.Iterable<T> collection,
Predicate<? super T> predicate)
Deprecated.
since 4.1, use
IterableUtils.find(Iterable, Predicate) instead |
static <T,C extends Closure<? super T>> |
forAllButLastDo(java.lang.Iterable<T> collection,
C closure)
Deprecated.
since 4.1, use
IterableUtils.forEachButLast(Iterable, Closure) instead |
static <T,C extends Closure<? super T>> |
forAllButLastDo(java.util.Iterator<T> iterator,
C closure)
Deprecated.
since 4.1, use
IteratorUtils.forEachButLast(Iterator, Closure) instead |
static <T,C extends Closure<? super T>> |
forAllDo(java.lang.Iterable<T> collection,
C closure)
Deprecated.
since 4.1, use
IterableUtils.forEach(Iterable, Closure) instead |
static <T,C extends Closure<? super T>> |
forAllDo(java.util.Iterator<T> iterator,
C closure)
Deprecated.
since 4.1, use
IteratorUtils.forEach(Iterator, Closure) instead |
static <T> T |
get(java.lang.Iterable<T> iterable,
int index)
Deprecated.
since 4.1, use
IterableUtils.get(Iterable, int) instead |
static <T> T |
get(java.util.Iterator<T> iterator,
int index)
Deprecated.
since 4.1, use
IteratorUtils.get(Iterator, int) instead |
static <K,V> java.util.Map.Entry<K,V> |
get(java.util.Map<K,V> map,
int index)
Returns the
index -th Map.Entry in the map 's entrySet ,
throwing IndexOutOfBoundsException if there is no such element. |
static java.lang.Object |
get(java.lang.Object object,
int index)
Returns the
index -th value in object , throwing
IndexOutOfBoundsException if there is no such element or
IllegalArgumentException if object is not an
instance of one of the supported types. |
static <O> java.util.Map<O,java.lang.Integer> |
getCardinalityMap(java.lang.Iterable<? extends O> coll)
Returns a
Map mapping each unique element in the given
Collection to an Integer representing the number
of occurrences of that element in the Collection . |
static <O> java.util.Collection<O> |
intersection(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b)
Returns a
Collection containing the intersection of the given
Iterable s. |
static boolean |
isEmpty(java.util.Collection<?> coll)
Null-safe check if the specified collection is empty.
|
static boolean |
isEqualCollection(java.util.Collection<?> a,
java.util.Collection<?> b)
Returns
true iff the given Collection s contain
exactly the same elements with exactly the same cardinalities. |
static <E> boolean |
isEqualCollection(java.util.Collection<? extends E> a,
java.util.Collection<? extends E> b,
Equator<? super E> equator)
Returns
true iff the given Collection s contain
exactly the same elements with exactly the same cardinalities. |
static boolean |
isFull(java.util.Collection<? extends java.lang.Object> coll)
Returns true if no more elements can be added to the Collection.
|
static boolean |
isNotEmpty(java.util.Collection<?> coll)
Null-safe check if the specified collection is not empty.
|
static boolean |
isProperSubCollection(java.util.Collection<?> a,
java.util.Collection<?> b)
Returns
true iff a is a proper sub-collection of b,
that is, iff the cardinality of e in a is less
than or equal to the cardinality of e in b,
for each element e in a, and there is at least one
element f such that the cardinality of f in b
is strictly greater than the cardinality of f in a. |
static boolean |
isSubCollection(java.util.Collection<?> a,
java.util.Collection<?> b)
Returns
true iff a is a sub-collection of b,
that is, iff the cardinality of e in a is less than or
equal to the cardinality of e in b, for each element e
in a. |
static <C> boolean |
matchesAll(java.lang.Iterable<C> input,
Predicate<? super C> predicate)
Deprecated.
since 4.1, use
IterableUtils.matchesAll(Iterable, Predicate) instead |
static int |
maxSize(java.util.Collection<? extends java.lang.Object> coll)
Get the maximum number of elements that the Collection can contain.
|
static <E> java.util.Collection<java.util.List<E>> |
permutations(java.util.Collection<E> collection)
Returns a
Collection of all the permutations of the input collection. |
static <C> java.util.Collection<C> |
predicatedCollection(java.util.Collection<C> collection,
Predicate<? super C> predicate)
Returns a predicated (validating) collection backed by the given collection.
|
static <E> java.util.Collection<E> |
removeAll(java.util.Collection<E> collection,
java.util.Collection<?> remove)
Removes the elements in
remove from collection . |
static <E> java.util.Collection<E> |
removeAll(java.lang.Iterable<E> collection,
java.lang.Iterable<? extends E> remove,
Equator<? super E> equator)
Removes all elements in
remove from collection . |
static <C> java.util.Collection<C> |
retainAll(java.util.Collection<C> collection,
java.util.Collection<?> retain)
Returns a collection containing all the elements in
collection
that are also in retain . |
static <E> java.util.Collection<E> |
retainAll(java.lang.Iterable<E> collection,
java.lang.Iterable<? extends E> retain,
Equator<? super E> equator)
Returns a collection containing all the elements in
collection that are also in retain . |
static void |
reverseArray(java.lang.Object[] array)
Reverses the order of the given array.
|
static <O> java.util.Collection<O> |
select(java.lang.Iterable<? extends O> inputCollection,
Predicate<? super O> predicate)
Selects all elements from input collection which match the given
predicate into an output collection.
|
static <O,R extends java.util.Collection<? super O>> |
select(java.lang.Iterable<? extends O> inputCollection,
Predicate<? super O> predicate,
R outputCollection)
Selects all elements from input collection which match the given
predicate and adds them to outputCollection.
|
static <O,R extends java.util.Collection<? super O>> |
select(java.lang.Iterable<? extends O> inputCollection,
Predicate<? super O> predicate,
R outputCollection,
R rejectedCollection)
Selects all elements from inputCollection into an output and rejected collection,
based on the evaluation of the given predicate.
|
static <O> java.util.Collection<O> |
selectRejected(java.lang.Iterable<? extends O> inputCollection,
Predicate<? super O> predicate)
Selects all elements from inputCollection which don't match the given
predicate into an output collection.
|
static <O,R extends java.util.Collection<? super O>> |
selectRejected(java.lang.Iterable<? extends O> inputCollection,
Predicate<? super O> predicate,
R outputCollection)
Selects all elements from inputCollection which don't match the given
predicate and adds them to outputCollection.
|
static int |
size(java.lang.Object object)
Gets the size of the collection/iterator specified.
|
static boolean |
sizeIsEmpty(java.lang.Object object)
Checks if the specified collection/array/iterator is empty.
|
static <O> java.util.Collection<O> |
subtract(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b)
Returns a new
Collection containing <i>a</i> - <i>b</i> . |
static <O> java.util.Collection<O> |
subtract(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b,
Predicate<O> p)
Returns a new
Collection containing a minus a subset of
b. |
static <C> java.util.Collection<C> |
synchronizedCollection(java.util.Collection<C> collection)
Deprecated.
since 4.1, use
Collections.synchronizedCollection(Collection) instead |
static <C> void |
transform(java.util.Collection<C> collection,
Transformer<? super C,? extends C> transformer)
Transform the collection by applying a Transformer to each element.
|
static <E> java.util.Collection<E> |
transformingCollection(java.util.Collection<E> collection,
Transformer<? super E,? extends E> transformer)
Returns a transformed bag backed by the given collection.
|
static <O> java.util.Collection<O> |
union(java.lang.Iterable<? extends O> a,
java.lang.Iterable<? extends O> b)
Returns a
Collection containing the union of the given
Iterable s. |
static <C> java.util.Collection<C> |
unmodifiableCollection(java.util.Collection<? extends C> collection)
Deprecated.
since 4.1, use
Collections.unmodifiableCollection(Collection) instead |
public static final java.util.Collection EMPTY_COLLECTION
public static <T> java.util.Collection<T> emptyCollection()
T
- the element typeEMPTY_COLLECTION
public static <T> java.util.Collection<T> emptyIfNull(java.util.Collection<T> collection)
null
,
or the argument itself otherwise.T
- the element typecollection
- the collection, possibly null
null
public static <O> java.util.Collection<O> union(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b)
Collection
containing the union of the given
Iterable
s.
The cardinality of each element in the returned Collection
will
be equal to the maximum of the cardinality of that element in the two
given Iterable
s.
O
- the generic type that is able to represent the types contained
in both input collections.a
- the first collection, must not be nullb
- the second collection, must not be nullCollection.addAll(java.util.Collection<? extends E>)
public static <O> java.util.Collection<O> intersection(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b)
Collection
containing the intersection of the given
Iterable
s.
The cardinality of each element in the returned Collection
will
be equal to the minimum of the cardinality of that element in the two
given Iterable
s.
O
- the generic type that is able to represent the types contained
in both input collections.a
- the first collection, must not be nullb
- the second collection, must not be nullCollection.retainAll(java.util.Collection<?>)
,
containsAny(java.util.Collection<?>, java.util.Collection<?>)
public static <O> java.util.Collection<O> disjunction(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b)
Collection
containing the exclusive disjunction
(symmetric difference) of the given Iterable
s.
The cardinality of each element e in the returned
Collection
will be equal to
max(cardinality(e,a),cardinality(e,b)) - min(cardinality(e,a),
cardinality(e,b)).
This is equivalent to
{@link #subtract subtract}({@link #union union(a,b)},{@link #intersection intersection(a,b)})
or
{@link #union union}({@link #subtract subtract(a,b)},{@link #subtract subtract(b,a)})
.
O
- the generic type that is able to represent the types contained
in both input collections.a
- the first collection, must not be nullb
- the second collection, must not be nullpublic static <O> java.util.Collection<O> subtract(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b)
Collection
containing <i>a</i> - <i>b</i>
.
The cardinality of each element e in the returned Collection
will be the cardinality of e in a minus the cardinality
of e in b, or zero, whichever is greater.O
- the generic type that is able to represent the types contained
in both input collections.a
- the collection to subtract from, must not be nullb
- the collection to subtract, must not be nullCollection.removeAll(java.util.Collection<?>)
public static <O> java.util.Collection<O> subtract(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b, Predicate<O> p)
Collection
containing a minus a subset of
b. Only the elements of b that satisfy the predicate
condition, p are subtracted from a.
The cardinality of each element e in the returned Collection
that satisfies the predicate condition will be the cardinality of e in a
minus the cardinality of e in b, or zero, whichever is greater.
The cardinality of each element e in the returned Collection
that does not
satisfy the predicate condition will be equal to the cardinality of e in a.
O
- the generic type that is able to represent the types contained
in both input collections.a
- the collection to subtract from, must not be nullb
- the collection to subtract, must not be nullp
- the condition used to determine which elements of b are
subtracted.Collection.removeAll(java.util.Collection<?>)
public static boolean containsAll(java.util.Collection<?> coll1, java.util.Collection<?> coll2)
true
iff all elements of coll2
are also contained
in coll1
. The cardinality of values in coll2
is not taken into account,
which is the same behavior as Collection.containsAll(Collection)
.
In other words, this method returns true
iff the
intersection(java.lang.Iterable<? extends O>, java.lang.Iterable<? extends O>)
of coll1 and coll2 has the same cardinality as
the set of unique values from coll2
. In case coll2
is empty, true
will be returned.
This method is intended as a replacement for Collection.containsAll(Collection)
with a guaranteed runtime complexity of O(n + m)
. Depending on the type of
Collection
provided, this method will be much faster than calling
Collection.containsAll(Collection)
instead, though this will come at the
cost of an additional space complexity O(n).
coll1
- the first collection, must not be nullcoll2
- the second collection, must not be nulltrue
iff the intersection of the collections has the same cardinality
as the set of unique elements from the second collectionpublic static boolean containsAny(java.util.Collection<?> coll1, java.util.Collection<?> coll2)
true
iff at least one element is in both collections.
In other words, this method returns true
iff the
intersection(java.lang.Iterable<? extends O>, java.lang.Iterable<? extends O>)
of coll1 and coll2 is not empty.
coll1
- the first collection, must not be nullcoll2
- the second collection, must not be nulltrue
iff the intersection of the collections is non-emptyintersection(java.lang.Iterable<? extends O>, java.lang.Iterable<? extends O>)
public static <O> java.util.Map<O,java.lang.Integer> getCardinalityMap(java.lang.Iterable<? extends O> coll)
Map
mapping each unique element in the given
Collection
to an Integer
representing the number
of occurrences of that element in the Collection
.
Only those elements present in the collection will appear as keys in the map.
O
- the type of object in the returned Map
. This is a super type of .coll
- the collection to get the cardinality map for, must not be nullpublic static boolean isSubCollection(java.util.Collection<?> a, java.util.Collection<?> b)
true
iff a is a sub-collection of b,
that is, iff the cardinality of e in a is less than or
equal to the cardinality of e in b, for each element e
in a.a
- the first (sub?) collection, must not be nullb
- the second (super?) collection, must not be nulltrue
iff a is a sub-collection of bisProperSubCollection(java.util.Collection<?>, java.util.Collection<?>)
,
Collection.containsAll(java.util.Collection<?>)
public static boolean isProperSubCollection(java.util.Collection<?> a, java.util.Collection<?> b)
true
iff a is a proper sub-collection of b,
that is, iff the cardinality of e in a is less
than or equal to the cardinality of e in b,
for each element e in a, and there is at least one
element f such that the cardinality of f in b
is strictly greater than the cardinality of f in a.
The implementation assumes
a.size()
and b.size()
represent the
total cardinality of a and b, resp. a.size() < Integer.MAXVALUE
a
- the first (sub?) collection, must not be nullb
- the second (super?) collection, must not be nulltrue
iff a is a proper sub-collection of bisSubCollection(java.util.Collection<?>, java.util.Collection<?>)
,
Collection.containsAll(java.util.Collection<?>)
public static boolean isEqualCollection(java.util.Collection<?> a, java.util.Collection<?> b)
true
iff the given Collection
s contain
exactly the same elements with exactly the same cardinalities.
That is, iff the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b.
a
- the first collection, must not be nullb
- the second collection, must not be nulltrue
iff the collections contain the same elements with the same cardinalities.public static <E> boolean isEqualCollection(java.util.Collection<? extends E> a, java.util.Collection<? extends E> b, Equator<? super E> equator)
true
iff the given Collection
s contain
exactly the same elements with exactly the same cardinalities.
That is, iff the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b.
Note: from version 4.1 onwards this method requires the input
collections and equator to be of compatible type (using bounded wildcards).
Providing incompatible arguments (e.g. by casting to their rawtypes)
will result in a ClassCastException
thrown at runtime.
E
- the element typea
- the first collection, must not be nullb
- the second collection, must not be nullequator
- the Equator used for testing equalitytrue
iff the collections contain the same elements with the same cardinalities.java.lang.NullPointerException
- if the equator is null@Deprecated public static <O> int cardinality(O obj, java.lang.Iterable<? super O> coll)
IterableUtils.frequency(Iterable, Object)
instead.
Be aware that the order of parameters has changed.O
- the type of object that the Iterable
may contain.obj
- the object to find the cardinality ofcoll
- the Iterable
to searchjava.lang.NullPointerException
- if coll is null@Deprecated public static <T> T find(java.lang.Iterable<T> collection, Predicate<? super T> predicate)
IterableUtils.find(Iterable, Predicate)
insteadIf the input collection or predicate is null, or no element of the collection matches the predicate, null is returned.
T
- the type of object the Iterable
containscollection
- the collection to search, may be nullpredicate
- the predicate to use, may be null@Deprecated public static <T,C extends Closure<? super T>> C forAllDo(java.lang.Iterable<T> collection, C closure)
IterableUtils.forEach(Iterable, Closure)
insteadIf the input collection or closure is null, there is no change made.
T
- the type of object the Iterable
containsC
- the closure typecollection
- the collection to get the input from, may be nullclosure
- the closure to perform, may be null@Deprecated public static <T,C extends Closure<? super T>> C forAllDo(java.util.Iterator<T> iterator, C closure)
IteratorUtils.forEach(Iterator, Closure)
insteadIf the input collection or closure is null, there is no change made.
T
- the type of object the Iterator
containsC
- the closure typeiterator
- the iterator to get the input from, may be nullclosure
- the closure to perform, may be null@Deprecated public static <T,C extends Closure<? super T>> T forAllButLastDo(java.lang.Iterable<T> collection, C closure)
IterableUtils.forEachButLast(Iterable, Closure)
insteadIf the input collection or closure is null, there is no change made.
T
- the type of object the Iterable
containsC
- the closure typecollection
- the collection to get the input from, may be nullclosure
- the closure to perform, may be null@Deprecated public static <T,C extends Closure<? super T>> T forAllButLastDo(java.util.Iterator<T> iterator, C closure)
IteratorUtils.forEachButLast(Iterator, Closure)
insteadIf the input collection or closure is null, there is no change made.
T
- the type of object the Collection
containsC
- the closure typeiterator
- the iterator to get the input from, may be nullclosure
- the closure to perform, may be nullpublic static <T> boolean filter(java.lang.Iterable<T> collection, Predicate<? super T> predicate)
If the input collection or predicate is null, there is no change made.
T
- the type of object the Iterable
containscollection
- the collection to get the input from, may be nullpredicate
- the predicate to use as a filter, may be nullpublic static <T> boolean filterInverse(java.lang.Iterable<T> collection, Predicate<? super T> predicate)
This is equivalent to
filter(collection, PredicateUtils.notPredicate(predicate))if predicate is != null.
If the input collection or predicate is null, there is no change made.
T
- the type of object the Iterable
containscollection
- the collection to get the input from, may be nullpredicate
- the predicate to use as a filter, may be nullpublic static <C> void transform(java.util.Collection<C> collection, Transformer<? super C,? extends C> transformer)
If the input collection or transformer is null, there is no change made.
This routine is best for Lists, for which set() is used to do the transformations "in place." For other Collections, clear() and addAll() are used to replace elements.
If the input collection controls its input, such as a Set, and the Transformer creates duplicates (or are otherwise invalid), the collection may reduce in size due to calling this method.
C
- the type of object the Collection
containscollection
- the Collection
to get the input from, may be nulltransformer
- the transformer to perform, may be null@Deprecated public static <C> int countMatches(java.lang.Iterable<C> input, Predicate<? super C> predicate)
IterableUtils.countMatches(Iterable, Predicate)
instead
A null
collection or predicate matches no elements.
C
- the type of object the Iterable
containsinput
- the Iterable
to get the input from, may be nullpredicate
- the predicate to use, may be null@Deprecated public static <C> boolean exists(java.lang.Iterable<C> input, Predicate<? super C> predicate)
IterableUtils.matchesAny(Iterable, Predicate)
instead
A null
collection or predicate returns false.
C
- the type of object the Iterable
containsinput
- the Iterable
to get the input from, may be nullpredicate
- the predicate to use, may be null@Deprecated public static <C> boolean matchesAll(java.lang.Iterable<C> input, Predicate<? super C> predicate)
IterableUtils.matchesAll(Iterable, Predicate)
instead
A null
predicate returns false.
A null
or empty collection returns true.
C
- the type of object the Iterable
containsinput
- the Iterable
to get the input from, may be nullpredicate
- the predicate to use, may be nullpublic static <O> java.util.Collection<O> select(java.lang.Iterable<? extends O> inputCollection, Predicate<? super O> predicate)
A null
predicate matches no elements.
O
- the type of object the Iterable
containsinputCollection
- the collection to get the input from, may not be nullpredicate
- the predicate to use, may be nulljava.lang.NullPointerException
- if the input collection is nullpublic static <O,R extends java.util.Collection<? super O>> R select(java.lang.Iterable<? extends O> inputCollection, Predicate<? super O> predicate, R outputCollection)
If the input collection or predicate is null, there is no change to the output collection.
O
- the type of object the Iterable
containsR
- the type of the output Collection
inputCollection
- the collection to get the input from, may be nullpredicate
- the predicate to use, may be nulloutputCollection
- the collection to output into, may not be null if the inputCollection
and predicate or not nullpublic static <O,R extends java.util.Collection<? super O>> R select(java.lang.Iterable<? extends O> inputCollection, Predicate<? super O> predicate, R outputCollection, R rejectedCollection)
Elements matching the predicate are added to the outputCollection
,
all other elements are added to the rejectedCollection
.
If the input predicate is null
, no elements are added to
outputCollection
or rejectedCollection
.
Note: calling the method is equivalent to the following code snippet:
select(inputCollection, predicate, outputCollection); selectRejected(inputCollection, predicate, rejectedCollection);
O
- the type of object the Iterable
containsR
- the type of the output Collection
inputCollection
- the collection to get the input from, may be nullpredicate
- the predicate to use, may be nulloutputCollection
- the collection to output selected elements into, may not be null if the
inputCollection and predicate are not nullrejectedCollection
- the collection to output rejected elements into, may not be null if the
inputCollection or predicate are not nullpublic static <O> java.util.Collection<O> selectRejected(java.lang.Iterable<? extends O> inputCollection, Predicate<? super O> predicate)
If the input predicate is null
, the result is an empty
list.
O
- the type of object the Iterable
containsinputCollection
- the collection to get the input from, may not be nullpredicate
- the predicate to use, may be nulljava.lang.NullPointerException
- if the input collection is nullpublic static <O,R extends java.util.Collection<? super O>> R selectRejected(java.lang.Iterable<? extends O> inputCollection, Predicate<? super O> predicate, R outputCollection)
If the input predicate is null
, no elements are added to
outputCollection
.
O
- the type of object the Iterable
containsR
- the type of the output Collection
inputCollection
- the collection to get the input from, may be nullpredicate
- the predicate to use, may be nulloutputCollection
- the collection to output into, may not be null if the inputCollection
and predicate or not nullpublic static <I,O> java.util.Collection<O> collect(java.lang.Iterable<I> inputCollection, Transformer<? super I,? extends O> transformer)
If the input collection or transformer is null, the result is an empty list.
I
- the type of object in the input collectionO
- the type of object in the output collectioninputCollection
- the collection to get the input from, may not be nulltransformer
- the transformer to use, may be nulljava.lang.NullPointerException
- if the input collection is nullpublic static <I,O> java.util.Collection<O> collect(java.util.Iterator<I> inputIterator, Transformer<? super I,? extends O> transformer)
If the input iterator or transformer is null, the result is an empty list.
I
- the type of object in the input collectionO
- the type of object in the output collectioninputIterator
- the iterator to get the input from, may be nulltransformer
- the transformer to use, may be nullpublic static <I,O,R extends java.util.Collection<? super O>> R collect(java.lang.Iterable<? extends I> inputCollection, Transformer<? super I,? extends O> transformer, R outputCollection)
If the input collection or transformer is null, there is no change to the output collection.
I
- the type of object in the input collectionO
- the type of object in the output collectionR
- the type of the output collectioninputCollection
- the collection to get the input from, may be nulltransformer
- the transformer to use, may be nulloutputCollection
- the collection to output into, may not be null if inputCollection
and transformer are not nulljava.lang.NullPointerException
- if the outputCollection is null and both, inputCollection and
transformer are not nullpublic static <I,O,R extends java.util.Collection<? super O>> R collect(java.util.Iterator<? extends I> inputIterator, Transformer<? super I,? extends O> transformer, R outputCollection)
If the input iterator or transformer is null, there is no change to the output collection.
I
- the type of object in the input collectionO
- the type of object in the output collectionR
- the type of the output collectioninputIterator
- the iterator to get the input from, may be nulltransformer
- the transformer to use, may be nulloutputCollection
- the collection to output into, may not be null if inputIterator
and transformer are not nulljava.lang.NullPointerException
- if the output collection is null and both, inputIterator and
transformer are not nullpublic static <T> boolean addIgnoreNull(java.util.Collection<T> collection, T object)
T
- the type of object the Collection
containscollection
- the collection to add to, must not be nullobject
- the object to add, if null it will not be addedjava.lang.NullPointerException
- if the collection is nullpublic static <C> boolean addAll(java.util.Collection<C> collection, java.lang.Iterable<? extends C> iterable)
Iterable
to the given collection. If the
Iterable
is a Collection
then it is cast and will be
added using Collection.addAll(Collection)
instead of iterating.C
- the type of object the Collection
containscollection
- the collection to add to, must not be nulliterable
- the iterable of elements to add, must not be nulljava.lang.NullPointerException
- if the collection or iterator is nullpublic static <C> boolean addAll(java.util.Collection<C> collection, java.util.Iterator<? extends C> iterator)
C
- the type of object the Collection
containscollection
- the collection to add to, must not be nulliterator
- the iterator of elements to add, must not be nulljava.lang.NullPointerException
- if the collection or iterator is nullpublic static <C> boolean addAll(java.util.Collection<C> collection, java.util.Enumeration<? extends C> enumeration)
C
- the type of object the Collection
containscollection
- the collection to add to, must not be nullenumeration
- the enumeration of elements to add, must not be nulltrue
if the collections was changed, false
otherwisejava.lang.NullPointerException
- if the collection or enumeration is nullpublic static <C> boolean addAll(java.util.Collection<C> collection, C[] elements)
C
- the type of object the Collection
containscollection
- the collection to add to, must not be nullelements
- the array of elements to add, must not be nulltrue
if the collection was changed, false
otherwisejava.lang.NullPointerException
- if the collection or array is null@Deprecated public static <T> T get(java.util.Iterator<T> iterator, int index)
IteratorUtils.get(Iterator, int)
insteadindex
-th value in Iterator
, throwing
IndexOutOfBoundsException
if there is no such element.
The Iterator is advanced to index
(or to the end, if
index
exceeds the number of entries) as a side effect of this method.
T
- the type of object in the Iterator
iterator
- the iterator to get a value fromindex
- the index to getjava.lang.IndexOutOfBoundsException
- if the index is invalidjava.lang.IllegalArgumentException
- if the object type is invalid@Deprecated public static <T> T get(java.lang.Iterable<T> iterable, int index)
IterableUtils.get(Iterable, int)
insteadindex
-th value in the iterable
's Iterator
, throwing
IndexOutOfBoundsException
if there is no such element.
If the Iterable
is a List
, then it will use List.get(int)
.
T
- the type of object in the Iterable
.iterable
- the Iterable
to get a value fromindex
- the index to getjava.lang.IndexOutOfBoundsException
- if the index is invalidpublic static java.lang.Object get(java.lang.Object object, int index)
index
-th value in object
, throwing
IndexOutOfBoundsException
if there is no such element or
IllegalArgumentException
if object
is not an
instance of one of the supported types.
The supported types, and associated semantics are:
Map.Entry
in position
index
in the map's entrySet
iterator,
if there is such an entry.index
-th array entry is returned,
if there is such an entry; otherwise an IndexOutOfBoundsException
is thrown.index
-th object
returned by the collection's default iterator, if there is such an element.index
-th object in the Iterator/Enumeration, if there
is such an element. The Iterator/Enumeration is advanced to
index
(or to the end, if index
exceeds the
number of entries) as a side effect of this method.object
- the object to get a value fromindex
- the index to getjava.lang.IndexOutOfBoundsException
- if the index is invalidjava.lang.IllegalArgumentException
- if the object type is invalidpublic static <K,V> java.util.Map.Entry<K,V> get(java.util.Map<K,V> map, int index)
index
-th Map.Entry
in the map
's entrySet
,
throwing IndexOutOfBoundsException
if there is no such element.K
- the key type in the Map
V
- the key type in the Map
map
- the object to get a value fromindex
- the index to getjava.lang.IndexOutOfBoundsException
- if the index is invalidpublic static int size(java.lang.Object object)
This method can handles objects as follows
object
- the object to get the size of, may be nulljava.lang.IllegalArgumentException
- thrown if object is not recognisedpublic static boolean sizeIsEmpty(java.lang.Object object)
This method can handles objects as follows
Note: This method is named to avoid clashing with
isEmpty(Collection)
.
object
- the object to get the size of, may be nulljava.lang.IllegalArgumentException
- thrown if object is not recognisedpublic static boolean isEmpty(java.util.Collection<?> coll)
Null returns true.
coll
- the collection to check, may be nullpublic static boolean isNotEmpty(java.util.Collection<?> coll)
Null returns false.
coll
- the collection to check, may be nullpublic static void reverseArray(java.lang.Object[] array)
array
- the array to reversepublic static boolean isFull(java.util.Collection<? extends java.lang.Object> coll)
This method uses the BoundedCollection
interface to determine the
full status. If the collection does not implement this interface then
false is returned.
The collection does not have to implement this interface directly. If the collection has been decorated using the decorators subpackage then these will be removed to access the BoundedCollection.
coll
- the collection to checkjava.lang.NullPointerException
- if the collection is nullpublic static int maxSize(java.util.Collection<? extends java.lang.Object> coll)
This method uses the BoundedCollection
interface to determine the
maximum size. If the collection does not implement this interface then
-1 is returned.
The collection does not have to implement this interface directly. If the collection has been decorated using the decorators subpackage then these will be removed to access the BoundedCollection.
coll
- the collection to checkjava.lang.NullPointerException
- if the collection is nullpublic static <O extends java.lang.Comparable<? super O>> java.util.List<O> collate(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b)
Uses the standard O(n) merge algorithm for combining two sorted lists.
O
- the element typea
- the first collection, must not be nullb
- the second collection, must not be nulljava.lang.NullPointerException
- if either collection is nullpublic static <O extends java.lang.Comparable<? super O>> java.util.List<O> collate(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b, boolean includeDuplicates)
Uses the standard O(n) merge algorithm for combining two sorted lists.
O
- the element typea
- the first collection, must not be nullb
- the second collection, must not be nullincludeDuplicates
- if true
duplicate elements will be retained, otherwise
they will be removed in the output collectionjava.lang.NullPointerException
- if either collection is nullpublic static <O> java.util.List<O> collate(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b, java.util.Comparator<? super O> c)
Uses the standard O(n) merge algorithm for combining two sorted lists.
O
- the element typea
- the first collection, must not be nullb
- the second collection, must not be nullc
- the comparator to use for the merge.java.lang.NullPointerException
- if either collection or the comparator is nullpublic static <O> java.util.List<O> collate(java.lang.Iterable<? extends O> a, java.lang.Iterable<? extends O> b, java.util.Comparator<? super O> c, boolean includeDuplicates)
Uses the standard O(n) merge algorithm for combining two sorted lists.
O
- the element typea
- the first collection, must not be nullb
- the second collection, must not be nullc
- the comparator to use for the merge.includeDuplicates
- if true
duplicate elements will be retained, otherwise
they will be removed in the output collectionjava.lang.NullPointerException
- if either collection or the comparator is nullpublic static <E> java.util.Collection<java.util.List<E>> permutations(java.util.Collection<E> collection)
Collection
of all the permutations of the input collection.
NOTE: the number of permutations of a given collection is equal to n!, where n is the size of the collection. Thus, the resulting collection will become very large for collections > 10 (e.g. 10! = 3628800, 15! = 1307674368000).
For larger collections it is advised to use a PermutationIterator
to
iterate over all permutations.
E
- the element typecollection
- the collection to create permutations for, may not be nulljava.lang.NullPointerException
- if collection is nullPermutationIterator
public static <C> java.util.Collection<C> retainAll(java.util.Collection<C> collection, java.util.Collection<?> retain)
collection
that are also in retain
. The cardinality of an element e
in the returned collection is the same as the cardinality of e
in collection
unless retain
does not contain e
, in which
case the cardinality is zero. This method is useful if you do not wish to modify
the collection c
and thus cannot call c.retainAll(retain);
.
This implementation iterates over collection
, checking each element in
turn to see if it's contained in retain
. If it's contained, it's added
to the returned list. As a consequence, it is advised to use a collection type for
retain
that provides a fast (e.g. O(1)) implementation of
Collection.contains(Object)
.
C
- the type of object the Collection
containscollection
- the collection whose contents are the target of the #retailAll operationretain
- the collection containing the elements to be retained in the returned collectionCollection
containing all the elements of collection
that occur at least once in retain
.java.lang.NullPointerException
- if either parameter is nullpublic static <E> java.util.Collection<E> retainAll(java.lang.Iterable<E> collection, java.lang.Iterable<? extends E> retain, Equator<? super E> equator)
collection
that are also in retain
. The
cardinality of an element e
in the returned collection is
the same as the cardinality of e
in collection
unless retain
does not contain e
, in which case
the cardinality is zero. This method is useful if you do not wish to
modify the collection c
and thus cannot call
c.retainAll(retain);
.
Moreover this method uses an Equator
instead of
Object.equals(Object)
to determine the equality of the elements
in collection
and retain
. Hence this method is
useful in cases where the equals behavior of an object needs to be
modified without changing the object itself.
E
- the type of object the Collection
containscollection
- the collection whose contents are the target of the retainAll
operationretain
- the collection containing the elements to be retained in the returned collectionequator
- the Equator used for testing equalityCollection
containing all the elements of collection
that occur at least once in retain
according to the equator
java.lang.NullPointerException
- if any of the parameters is nullpublic static <E> java.util.Collection<E> removeAll(java.util.Collection<E> collection, java.util.Collection<?> remove)
remove
from collection
. That is, this
method returns a collection containing all the elements in c
that are not in remove
. The cardinality of an element e
in the returned collection is the same as the cardinality of e
in collection
unless remove
contains e
, in which
case the cardinality is zero. This method is useful if you do not wish to modify
the collection c
and thus cannot call collection.removeAll(remove);
.
This implementation iterates over collection
, checking each element in
turn to see if it's contained in remove
. If it's not contained, it's added
to the returned list. As a consequence, it is advised to use a collection type for
remove
that provides a fast (e.g. O(1)) implementation of
Collection.contains(Object)
.
E
- the type of object the Collection
containscollection
- the collection from which items are removed (in the returned collection)remove
- the items to be removed from the returned collection
Collection
containing all the elements of collection
except
any elements that also occur in remove
.java.lang.NullPointerException
- if either parameter is nullpublic static <E> java.util.Collection<E> removeAll(java.lang.Iterable<E> collection, java.lang.Iterable<? extends E> remove, Equator<? super E> equator)
remove
from collection
.
That is, this method returns a collection containing all the elements in
collection
that are not in remove
. The
cardinality of an element e
in the returned collection is
the same as the cardinality of e
in collection
unless remove
contains e
, in which case the
cardinality is zero. This method is useful if you do not wish to modify
the collection c
and thus cannot call
collection.removeAll(remove)
.
Moreover this method uses an Equator
instead of
Object.equals(Object)
to determine the equality of the elements
in collection
and remove
. Hence this method is
useful in cases where the equals behavior of an object needs to be
modified without changing the object itself.
E
- the type of object the Collection
containscollection
- the collection from which items are removed (in the returned collection)remove
- the items to be removed from the returned collectionequator
- the Equator used for testing equalityCollection
containing all the elements of collection
except any element that if equal according to the equator
java.lang.NullPointerException
- if any of the parameters is null@Deprecated public static <C> java.util.Collection<C> synchronizedCollection(java.util.Collection<C> collection)
Collections.synchronizedCollection(Collection)
insteadYou must manually synchronize on the returned buffer's iterator to avoid non-deterministic behavior:
Collection c = CollectionUtils.synchronizedCollection(myCollection); synchronized (c) { Iterator i = c.iterator(); while (i.hasNext()) { process (i.next()); } }This method uses the implementation in the decorators subpackage.
C
- the type of object the Collection
containscollection
- the collection to synchronize, must not be nulljava.lang.NullPointerException
- if the collection is null@Deprecated public static <C> java.util.Collection<C> unmodifiableCollection(java.util.Collection<? extends C> collection)
Collections.unmodifiableCollection(Collection)
insteadThis method uses the implementation in the decorators subpackage.
C
- the type of object the Collection
containscollection
- the collection to make unmodifiable, must not be nulljava.lang.NullPointerException
- if the collection is nullpublic static <C> java.util.Collection<C> predicatedCollection(java.util.Collection<C> collection, Predicate<? super C> predicate)
Only objects that pass the test in the given predicate can be added to the collection. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original collection after invoking this method, as it is a backdoor for adding invalid objects.
C
- the type of objects in the Collection.collection
- the collection to predicate, must not be nullpredicate
- the predicate for the collection, must not be nulljava.lang.NullPointerException
- if the Collection is nullpublic static <E> java.util.Collection<E> transformingCollection(java.util.Collection<E> collection, Transformer<? super E,? extends E> transformer)
Each object is passed through the transformer as it is added to the Collection. It is important not to use the original collection after invoking this method, as it is a backdoor for adding untransformed objects.
Existing entries in the specified collection will not be transformed.
If you want that behaviour, see TransformedCollection.transformedCollection(java.util.Collection<E>, org.apache.commons.collections4.Transformer<? super E, ? extends E>)
.
E
- the type of object the Collection
containscollection
- the collection to predicate, must not be nulltransformer
- the transformer for the collection, must not be nulljava.lang.NullPointerException
- if the Collection or Transformer is nullpublic static <E> E extractSingleton(java.util.Collection<E> collection)
E
- collection typecollection
- to readjava.lang.NullPointerException
- if collection is nulljava.lang.IllegalArgumentException
- if collection is empty or contains more than one element"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"