|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.commons.collections.CollectionUtils
A set of Collection
related utility methods.
Constructor Summary | |
CollectionUtils()
Please don't ever instantiate a CollectionUtils . |
Method Summary | |
static void |
addAll(Collection collection,
Enumeration enumeration)
Adds all elements in the enumeration to the given collection. |
static void |
addAll(Collection collection,
Iterator iterator)
Adds all elements in the iteration to the given collection. |
static void |
addAll(Collection collection,
Object[] elements)
Adds all elements in the array to the given collection. |
static int |
cardinality(Object obj,
Collection col)
Returns the number of occurrences of obj in col. |
static Collection |
collect(Collection inputCollection,
Transformer transformer)
Transforms all elements from inputCollection with the given transformer and adds them to the outputCollection. |
static Collection |
collect(Collection inputCollection,
Transformer transformer,
Collection outputCollection)
Transforms all elements from inputCollection with the given transformer and adds them to the outputCollection. |
static Collection |
collect(Iterator inputIterator,
Transformer transformer)
Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection. |
static Collection |
collect(Iterator inputIterator,
Transformer transformer,
Collection outputCollection)
Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection. |
static boolean |
containsAny(Collection a,
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 Collection |
disjunction(Collection a,
Collection b)
Returns a Collection containing the exclusive disjunction
(symmetric difference) of the given Collection s.
|
static void |
filter(Collection collection,
Predicate predicate)
Filter the collection by applying a Predicate to each element. |
static Object |
find(Collection collection,
Predicate predicate)
Finds the first element in the given collection which matches the given predicate. |
static void |
forAllDo(Collection collection,
Closure closure)
Executes the given closure on each element in the collection. |
static Map |
getCardinalityMap(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 Object |
index(Object obj,
int idx)
Given an Object, and an index, it will get the nth value in the object. |
static Object |
index(Object obj,
Object index)
Given an Object, and a key (index), it will get value associated with that key in the Object. |
static Collection |
intersection(Collection a,
Collection b)
Returns a Collection containing the intersection
of the given Collection s.
|
static boolean |
isEqualCollection(Collection a,
Collection b)
Returns true iff the given Collection s contain
exactly the same elements with exactly the same cardinality.
|
static boolean |
isProperSubCollection(Collection a,
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(Collection a,
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 Collection |
predicatedCollection(Collection collection,
Predicate predicate)
Returns a predicated collection backed by the given collection. |
static void |
reverseArray(Object[] array)
Reverses the order of the given array |
static Collection |
select(Collection inputCollection,
Predicate predicate)
Selects all elements from input collection which match the given predicate into an output collection. |
static void |
select(Collection inputCollection,
Predicate predicate,
Collection outputCollection)
Selects all elements from input collection which match the given predicate and adds them to outputCollection. |
static Collection |
subtract(Collection a,
Collection b)
Returns a Collection containing a - b.
|
static void |
transform(Collection collection,
Transformer transformer)
Transform the collection by applying a Transformer to each element. |
static Collection |
union(Collection a,
Collection b)
Returns a Collection containing the union
of the given Collection s.
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public CollectionUtils()
CollectionUtils
.
Method Detail |
public static void addAll(Collection collection, Enumeration enumeration)
collection
- the collection to add toenumeration
- the enumeration of elements to add, may not be null
NullPointerException
- if the collection or enumeration is nullpublic static void addAll(Collection collection, Iterator iterator)
collection
- the collection to add toiterator
- the iterator of elements to add, may not be null
NullPointerException
- if the collection or iterator is nullpublic static void addAll(Collection collection, Object[] elements)
collection
- the collection to add toelements
- the array of elements to add, may be null
NullPointerException
- if the collection or array is nullpublic static int cardinality(Object obj, Collection col)
public static Collection collect(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 null
NullPointerException
- if the input collection is nullpublic static Collection collect(Collection inputCollection, Transformer transformer, 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 null
NullPointerException
- if the output collection is nullpublic static Collection collect(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 null
public static Collection collect(Iterator inputIterator, Transformer transformer, 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 null
NullPointerException
- if the output collection is nullpublic static boolean containsAny(Collection a, 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
Collection
true
iff the intersection of a and b is non-emptyintersection(java.util.Collection, java.util.Collection)
public static Collection disjunction(Collection a, 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(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 Object find(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 null
public static void forAllDo(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 null
NullPointerException
- if the closure is nullpublic static Map getCardinalityMap(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 Object index(Object obj, int idx)
obj
- the object to get an index of
IndexOutOfBoundsException
NoSuchElementException
public static Object index(Object obj, Object index)
obj
- the object to get an index ofindex
- the index to get
IndexOutOfBoundsException
NoSuchElementException
public static Collection intersection(Collection a, 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(Collection a, 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(Collection a, Collection b)
isSubCollection(java.util.Collection, java.util.Collection)
,
Collection.containsAll(java.util.Collection)
public static boolean isSubCollection(Collection a, Collection b)
isProperSubCollection(java.util.Collection, java.util.Collection)
,
Collection.containsAll(java.util.Collection)
public static Collection predicatedCollection(Collection collection, Predicate predicate)
collection
- the collection to predicate, must not be nullpredicate
- the predicate for the collection, must not be null
IllegalArgumentException
- if the Collection is nullpublic static void reverseArray(Object[] array)
public static Collection select(Collection inputCollection, Predicate predicate)
inputCollection
- the collection to get the input from, may not be nullpredicate
- the predicate to use, may be null
NullPointerException
- if the input collection is nullpublic static void select(Collection inputCollection, Predicate predicate, 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 null
NullPointerException
- if the input collection is nullpublic static Collection subtract(Collection a, 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(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 Collection union(Collection a, 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)
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |