org.apache.commons.collections
public class CollectionUtils extends java.lang.Object
Collection
related utility methods.Constructor and Description |
---|
CollectionUtils()
Please don't ever instantiate a
CollectionUtils . |
Modifier and Type | Method and Description |
---|---|
static void |
addAll(java.util.Collection collection,
java.util.Enumeration enumeration)
Adds all elements in the enumeration to the given collection.
|
static void |
addAll(java.util.Collection collection,
java.util.Iterator iterator)
Adds all elements in the iteration to the given collection.
|
static void |
addAll(java.util.Collection collection,
java.lang.Object[] elements)
Adds all elements in the array to the given collection.
|
static int |
cardinality(java.lang.Object obj,
java.util.Collection col)
Returns the number of occurrences of obj
in col.
|
static java.util.Collection |
collect(java.util.Collection inputCollection,
Transformer transformer)
Transforms all elements from inputCollection with the given transformer
and adds them to the outputCollection.
|
static java.util.Collection |
collect(java.util.Collection inputCollection,
Transformer transformer,
java.util.Collection outputCollection)
Transforms all elements from inputCollection with the given transformer
and adds them to the outputCollection.
|
static java.util.Collection |
collect(java.util.Iterator inputIterator,
Transformer transformer)
Transforms all elements from the inputIterator with the given transformer
and adds them to the outputCollection.
|
static java.util.Collection |
collect(java.util.Iterator inputIterator,
Transformer transformer,
java.util.Collection outputCollection)
Transforms all elements from the inputIterator with the given transformer
and adds them to the outputCollection.
|
static boolean |
containsAny(java.util.Collection a,
java.util.Collection b)
Returns
true iff some element of a
is also an element of b (or, equivalently, if
some element of b is also an element of a). |
static java.util.Collection |
disjunction(java.util.Collection a,
java.util.Collection b)
Returns a
Collection containing the exclusive disjunction
(symmetric difference) of the given Collection s. |
static void |
filter(java.util.Collection collection,
Predicate predicate)
Filter the collection by applying a Predicate to each element.
|
static java.lang.Object |
find(java.util.Collection collection,
Predicate predicate)
Finds the first element in the given collection which matches the given predicate.
|
static void |
forAllDo(java.util.Collection collection,
Closure closure)
Executes the given closure on each element in the collection.
|
static java.util.Map |
getCardinalityMap(java.util.Collection col)
Returns a
Map mapping each unique element in
the given Collection to an Integer
representing the number of occurances of that element
in the Collection . |
static java.lang.Object |
index(java.lang.Object obj,
int idx)
Given an Object, and an index, it will get the nth value in the
object.
|
static java.lang.Object |
index(java.lang.Object obj,
java.lang.Object index)
Given an Object, and a key (index), it will get value associated with
that key in the Object.
|
static java.util.Collection |
intersection(java.util.Collection a,
java.util.Collection b)
Returns a
Collection containing the intersection
of the given Collection s. |
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 cardinality. |
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 java.util.Collection |
predicatedCollection(java.util.Collection collection,
Predicate predicate)
Returns a predicated collection backed by the given collection.
|
static void |
reverseArray(java.lang.Object[] array)
Reverses the order of the given array
|
static java.util.Collection |
select(java.util.Collection inputCollection,
Predicate predicate)
Selects all elements from input collection which match the given predicate
into an output collection.
|
static void |
select(java.util.Collection inputCollection,
Predicate predicate,
java.util.Collection outputCollection)
Selects all elements from input collection which match the given predicate
and adds them to outputCollection.
|
static java.util.Collection |
subtract(java.util.Collection a,
java.util.Collection b)
Returns a
Collection containing a - b. |
static void |
transform(java.util.Collection collection,
Transformer transformer)
Transform the collection by applying a Transformer to each element.
|
static java.util.Collection |
union(java.util.Collection a,
java.util.Collection b)
Returns a
Collection containing the union
of the given Collection s. |
public CollectionUtils()
CollectionUtils
.public static void addAll(java.util.Collection collection, java.util.Enumeration enumeration)
collection
- the collection to add toenumeration
- the enumeration of elements to add, may not be nulljava.lang.NullPointerException
- if the collection or enumeration is nullpublic static void addAll(java.util.Collection collection, java.util.Iterator iterator)
collection
- the collection to add toiterator
- the iterator of elements to add, may not be nulljava.lang.NullPointerException
- if the collection or iterator is nullpublic static void addAll(java.util.Collection collection, java.lang.Object[] elements)
collection
- the collection to add toelements
- the array of elements to add, may be nulljava.lang.NullPointerException
- if the collection or array is nullpublic static int cardinality(java.lang.Object obj, java.util.Collection col)
public static java.util.Collection collect(java.util.Collection inputCollection, Transformer transformer)
If the input transfomer is null, the result is an empty list.
inputCollection
- 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 java.util.Collection collect(java.util.Collection inputCollection, Transformer transformer, java.util.Collection outputCollection)
If the input collection or transfomer is null, there is no change to the output collection.
inputCollection
- 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 nulljava.lang.NullPointerException
- if the output collection is nullpublic static java.util.Collection collect(java.util.Iterator inputIterator, Transformer transformer)
If the input iterator or transfomer is null, the result is an empty list.
inputIterator
- the iterator to get the input from, may be nulltransformer
- the transformer to use, may be nullpublic static java.util.Collection collect(java.util.Iterator inputIterator, Transformer transformer, java.util.Collection outputCollection)
If the input iterator or transfomer is null, there is no change to the output collection.
inputIterator
- 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 nulljava.lang.NullPointerException
- if the output collection is nullpublic static boolean containsAny(java.util.Collection a, java.util.Collection b)
true
iff some element of a
is also an element of b (or, equivalently, if
some element of b is also an element of a).
In other words, this method returns true
iff the intersection(java.util.Collection, java.util.Collection)
of a and b
is not empty.a
- a non-null
Collectionb
- a non-null
Collectiontrue
iff the intersection of a and b is non-emptyintersection(java.util.Collection, java.util.Collection)
public static java.util.Collection disjunction(java.util.Collection a, java.util.Collection b)
Collection
containing the exclusive disjunction
(symmetric difference) of the given Collection
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
subtract
(union(a,b)
,intersection(a,b)
)
or
union
(subtract(a,b)
,subtract(b,a)
).
public static void filter(java.util.Collection collection, Predicate predicate)
If the input collection or predicate is null, there is no change made.
collection
- the collection to get the input from, may be nullpredicate
- the predicate to use as a filter, may be nullpublic static java.lang.Object find(java.util.Collection collection, Predicate predicate)
If the input collection or predicate is null, null is returned.
collection
- the collection to search, may be nullpredicate
- the predicate to use, may be nullpublic static void forAllDo(java.util.Collection collection, Closure closure)
If the input collection is null, there is no change made.
collection
- the collection to get the input from, may be nullclosure
- the closure to perform, may not be nulljava.lang.NullPointerException
- if the closure is nullpublic static java.util.Map getCardinalityMap(java.util.Collection col)
Map
mapping each unique element in
the given Collection
to an Integer
representing the number of occurances of that element
in the Collection
.
An entry that maps to null indicates that the
element does not appear in the given Collection
.public static java.lang.Object index(java.lang.Object obj, int idx)
obj
- the object to get an index ofindex
- the index to getjava.lang.IndexOutOfBoundsException
java.util.NoSuchElementException
public static java.lang.Object index(java.lang.Object obj, java.lang.Object index)
obj
- the object to get an index ofindex
- the index to getjava.lang.IndexOutOfBoundsException
java.util.NoSuchElementException
public static java.util.Collection intersection(java.util.Collection a, java.util.Collection b)
Collection
containing the intersection
of the given Collection
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 Collection
s.
Collection.retainAll(java.util.Collection>)
,
containsAny(java.util.Collection, java.util.Collection)
public static boolean isEqualCollection(java.util.Collection a, java.util.Collection b)
Collection
s contain
exactly the same elements with exactly the same cardinality.
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.
public static boolean isProperSubCollection(java.util.Collection a, java.util.Collection b)
isSubCollection(java.util.Collection, java.util.Collection)
,
Collection.containsAll(java.util.Collection>)
public static boolean isSubCollection(java.util.Collection a, java.util.Collection b)
isProperSubCollection(java.util.Collection, java.util.Collection)
,
Collection.containsAll(java.util.Collection>)
public static java.util.Collection predicatedCollection(java.util.Collection collection, Predicate predicate)
collection
- the collection to predicate, must not be nullpredicate
- the predicate for the collection, must not be nulljava.lang.IllegalArgumentException
- if the Collection is nullpublic static void reverseArray(java.lang.Object[] array)
public static java.util.Collection select(java.util.Collection inputCollection, Predicate predicate)
inputCollection
- 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 void select(java.util.Collection inputCollection, Predicate predicate, java.util.Collection outputCollection)
If the input collection or predicate is null, there is no change to 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 nulljava.lang.NullPointerException
- if the input collection is nullpublic static java.util.Collection subtract(java.util.Collection a, java.util.Collection b)
Collection
containing a - b.
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.Collection.removeAll(java.util.Collection>)
public static void transform(java.util.Collection collection, Transformer transformer)
If the input collection or transformer is null, there is no change made.
This routine is best for Lists and uses set(), however it adapts for all Collections that support clear() and addAll().
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.
collection
- the collection to get the input from, may be nulltransformer
- the transformer to perform, may be nullpublic static java.util.Collection union(java.util.Collection a, java.util.Collection b)
Collection
containing the union
of the given Collection
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 Collection
s.
Collection.addAll(java.util.Collection extends E>)
Copyright © 2001-2004 Apache Software Foundation. Documenation generated February 26 2016.