Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
java.lang.Object
org.apache.commons.collections.CollectionUtils
public class CollectionUtils
extends java.lang.Object
Collection
related utility methods.
Field Summary | |
static Iterator |
|
Constructor Summary | |
|
Method Summary | |
static void |
|
static void |
|
static void |
|
static int |
|
static Collection |
|
static Collection |
|
static Collection |
|
static Collection |
|
static boolean |
|
static Collection |
|
static void | |
static Object | |
static void | |
static Map |
|
static Iterator |
|
static Object |
|
static Object |
|
static Collection |
|
static boolean |
|
static boolean |
|
static boolean |
|
static Collection |
|
static void |
|
static Collection | |
static void | |
static Collection |
|
static void |
|
static Collection |
|
public static final Iterator EMPTY_ITERATOR
Deprecated. use IteratorUtils.EMPTY_ITERATOR
The empty iterator (immutable).
public static void addAll(Collection collection, Enumeration enumeration)
Adds all elements in the enumeration to the given collection.
- Parameters:
collection
- the collection to add toenumeration
- the enumeration of elements to add, may not be null
public static void addAll(Collection collection, Iterator iterator)
Adds all elements in the iteration to the given collection.
- Parameters:
collection
- the collection to add toiterator
- the iterator of elements to add, may not be null
public static void addAll(Collection collection, Object[] elements)
Adds all elements in the array to the given collection.
- Parameters:
collection
- the collection to add toelements
- the array of elements to add, may be null
public static int cardinality(Object obj, Collection col)
Returns the number of occurrences of obj in col.
public static Collection collect(Collection inputCollection, Transformer transformer)
Transforms all elements from inputCollection with the given transformer and adds them to the outputCollection. If the input transfomer is null, the result is an empty list.
- Parameters:
inputCollection
- the collection to get the input from, may not be nulltransformer
- the transformer to use, may be null
- Returns:
- the transformed result (new list)
public static Collection collect(Collection inputCollection, Transformer transformer, Collection outputCollection)
Transforms all elements from inputCollection with the given transformer and adds them to the outputCollection. If the input collection or transfomer is null, there is no change to the output collection.
- Parameters:
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
- Returns:
- the outputCollection with the transformed input added
public static Collection collect(Iterator inputIterator, Transformer transformer)
Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection. If the input iterator or transfomer is null, the result is an empty list.
- Parameters:
inputIterator
- the iterator to get the input from, may be nulltransformer
- the transformer to use, may be null
- Returns:
- the transformed result (new list)
public 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. If the input iterator or transfomer is null, there is no change to the output collection.
- Parameters:
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
- Returns:
- the outputCollection with the transformed input added
public static boolean containsAny(Collection a, Collection b)
Returnstrue
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 returnstrue
iff theintersection(Collection,Collection)
of a and b is not empty.
- Parameters:
a
- a non-null
Collectionb
- a non-null
Collection
- Returns:
true
iff the intersection of a and b is non-empty
- Since:
- 2.1
- See Also:
intersection(Collection,Collection)
public static Collection disjunction(Collection a, Collection b)
Returns aCollection
containing the exclusive disjunction (symmetric difference) of the givenCollection
s. The cardinality of each element e in the returnedCollection
will be equal to max(cardinality(e,a),cardinality(e,b)) - min(cardinality(e,a),cardinality(e,b)). This is equivalent tosubtract
(union(a,b)
,intersection(a,b)
) orunion
(subtract(a,b)
,subtract(b,a)
).
public static void filter(Collection collection, Predicate predicate)
Filter the collection by applying a Predicate to each element. If the predicate returns false, remove the element. If the input collection or predicate is null, there is no change made.
- Parameters:
collection
- the collection to get the input from, may be nullpredicate
- the predicate to use as a filter, may be null
public static Object find(Collection collection, Predicate predicate)
Finds the first element in the given collection which matches the given predicate. If the input collection or predicate is null, null is returned.
- Parameters:
collection
- the collection to search, may be nullpredicate
- the predicate to use, may be null
- Returns:
- the first element of the collection which matches the predicate or null if none could be found
public static void forAllDo(Collection collection, Closure closure)
Executes the given closure on each element in the collection. If the input collection is null, there is no change made.
- Parameters:
collection
- the collection to get the input from, may be nullclosure
- the closure to perform, may not be null
public static Map getCardinalityMap(Collection col)
Returns aMap
mapping each unique element in the givenCollection
to anInteger
representing the number of occurances of that element in theCollection
. An entry that maps to null indicates that the element does not appear in the givenCollection
.
public static Iterator getIterator(Object obj)
Deprecated. use IteratorUtils version instead
Returns an Iterator for the given object. Currently this method can handle Iterator, Enumeration, Collection, Map, Object[] or array.
public 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. The following checks are made:
- If obj is a Map, use the index as a key to get a value. If no match continue.
- Check key is an Integer. If not, return the object passed in.
- If obj is a Map, get the nth value from the key iterator.
- If obj is a List or an array, get the nth value.
- If obj is an iterator, enumeration or Collection, get the nth value from the iterator.
- Return the original obj.
- Parameters:
obj
- the object to get an index ofindex
- the index to get
- Returns:
- the object at the specified index
public static Object index(Object obj, int idx)
Given an Object, and an index, it will get the nth value in the object.
- If obj is a Map, get the nth value from the key iterator.
- If obj is a List or an array, get the nth value.
- If obj is an iterator, enumeration or Collection, get the nth value from the iterator.
- Return the original obj.
- Parameters:
obj
- the object to get an index of
public static Collection intersection(Collection a, Collection b)
Returns aCollection
containing the intersection of the givenCollection
s. The cardinality of each element in the returnedCollection
will be equal to the minimum of the cardinality of that element in the two givenCollection
s.
- See Also:
Collection.retainAll
,containsAny(Collection,Collection)
public static boolean isEqualCollection(Collection a, Collection b)
Returns true iff the givenCollection
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)
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.
- See Also:
isSubCollection(Collection,Collection)
,Collection.containsAll
public 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.
- See Also:
isProperSubCollection(Collection,Collection)
,Collection.containsAll
public static Collection predicatedCollection(Collection collection, Predicate predicate)
Returns a predicated collection backed by the given collection. Only objects that pass the test in the given predicate can be added to the collection. It is important not to use the original collection after invoking this method, as it is a backdoor for adding unvalidated objects.
- Parameters:
collection
- the collection to predicate, must not be nullpredicate
- the predicate for the collection, must not be null
- Returns:
- a predicated collection backed by the given collection
public static void reverseArray(Object[] array)
Reverses the order of the given array
public static Collection select(Collection inputCollection, Predicate predicate)
Selects all elements from input collection which match the given predicate into an output collection.
- Parameters:
inputCollection
- the collection to get the input from, may not be nullpredicate
- the predicate to use, may be null
- Returns:
- the elements matching the predicate (new list)
public 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. If the input collection or predicate is null, there is no change to the output collection.
- Parameters:
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
public static Collection subtract(Collection a, Collection b)
Returns aCollection
containing a - b. The cardinality of each element e in the returnedCollection
will be the cardinality of e in a minus the cardinality of e in b, or zero, whichever is greater.
- See Also:
Collection.removeAll
public static void transform(Collection collection, Transformer transformer)
Transform the collection by applying a Transformer to each element. 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.
- Parameters:
collection
- the collection to get the input from, may be nulltransformer
- the transformer to perform, may be null
public static Collection union(Collection a, Collection b)
Returns aCollection
containing the union of the givenCollection
s. The cardinality of each element in the returnedCollection
will be equal to the maximum of the cardinality of that element in the two givenCollection
s.
- See Also:
Collection.addAll