org.apache.commons.collections
Class CollectionUtils

java.lang.Object
  |
  +--org.apache.commons.collections.CollectionUtils

public class CollectionUtils
extends java.lang.Object

A set of Collection related utility methods.

Since:
1.0
Version:
$Revision: 1.18.2.2 $ $Date: 2004/05/22 12:14:02 $
Author:
Rodney Waldhoff, Paul Jack, Stephen Colebourne, Steve Downey

Field Summary
static java.util.Iterator EMPTY_ITERATOR
          Deprecated. use IteratorUtils.EMPTY_ITERATOR
 
Constructor Summary
CollectionUtils()
          Please don't ever instantiate a CollectionUtils.
 
Method Summary
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 Collections.
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.util.Iterator getIterator(java.lang.Object obj)
          Deprecated. use IteratorUtils version instead
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 Collections.
static boolean isEqualCollection(java.util.Collection a, java.util.Collection b)
          Returns true iff the given Collections 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 Collections.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ITERATOR

public static final java.util.Iterator EMPTY_ITERATOR
Deprecated. use IteratorUtils.EMPTY_ITERATOR

The empty iterator (immutable).

Constructor Detail

CollectionUtils

public CollectionUtils()
Please don't ever instantiate a CollectionUtils.

Method Detail

union

public static java.util.Collection union(java.util.Collection a,
                                         java.util.Collection b)
Returns a Collection containing the union of the given Collections.

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 Collections.

See Also:
Collection.addAll(java.util.Collection)

intersection

public static java.util.Collection intersection(java.util.Collection a,
                                                java.util.Collection b)
Returns a Collection containing the intersection of the given Collections.

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 Collections.

See Also:
Collection.retainAll(java.util.Collection), containsAny(java.util.Collection, java.util.Collection)

disjunction

public 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 Collections.

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)).


subtract

public static java.util.Collection subtract(java.util.Collection a,
                                            java.util.Collection b)
Returns a 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.

See Also:
Collection.removeAll(java.util.Collection)

containsAny

public 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). In other words, this method returns true iff the intersection(java.util.Collection, java.util.Collection) of a and b is not empty.

Parameters:
a - a non-null Collection
b - a non-null Collection
Returns:
true iff the intersection of a and b is non-empty
Since:
2.1
See Also:
intersection(java.util.Collection, java.util.Collection)

getCardinalityMap

public 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. An entry that maps to null indicates that the element does not appear in the given Collection.


isSubCollection

public 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.

See Also:
isProperSubCollection(java.util.Collection, java.util.Collection), Collection.containsAll(java.util.Collection)

isProperSubCollection

public 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.

See Also:
isSubCollection(java.util.Collection, java.util.Collection), Collection.containsAll(java.util.Collection)

isEqualCollection

public static boolean isEqualCollection(java.util.Collection a,
                                        java.util.Collection b)
Returns true iff the given Collections 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.


cardinality

public static int cardinality(java.lang.Object obj,
                              java.util.Collection col)
Returns the number of occurrences of obj in col.


find

public static java.lang.Object find(java.util.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 null
predicate - 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

forAllDo

public static void forAllDo(java.util.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 null
closure - the closure to perform, may not be null
Throws:
java.lang.NullPointerException - if the closure is null

filter

public static void filter(java.util.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 null
predicate - the predicate to use as a filter, may be null

transform

public static void transform(java.util.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 null
transformer - the transformer to perform, may be null

select

public 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.

Parameters:
inputCollection - the collection to get the input from, may not be null
predicate - the predicate to use, may be null
Returns:
the elements matching the predicate (new list)
Throws:
java.lang.NullPointerException - if the input collection is null

select

public 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.

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 null
predicate - the predicate to use, may be null
outputCollection - the collection to output into, may not be null
Returns:
the outputCollection with the the elements matching the predicate added
Throws:
java.lang.NullPointerException - if the input collection is null

collect

public 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.

If the input transfomer is null, the result is an empty list.

Parameters:
inputCollection - the collection to get the input from, may not be null
transformer - the transformer to use, may be null
Returns:
the transformed result (new list)
Throws:
java.lang.NullPointerException - if the input collection is null

collect

public 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.

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 null
transformer - the transformer to use, may be null
Returns:
the transformed result (new list)

collect

public 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.

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 null
transformer - the transformer to use, may be null
outputCollection - the collection to output into, may not be null
Returns:
the outputCollection with the transformed input added
Throws:
java.lang.NullPointerException - if the output collection is null

collect

public 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.

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 null
transformer - the transformer to use, may be null
outputCollection - the collection to output into, may not be null
Returns:
the outputCollection with the transformed input added
Throws:
java.lang.NullPointerException - if the output collection is null

addAll

public static void addAll(java.util.Collection collection,
                          java.util.Iterator iterator)
Adds all elements in the iteration to the given collection.

Parameters:
collection - the collection to add to
iterator - the iterator of elements to add, may not be null
Throws:
java.lang.NullPointerException - if the collection or iterator is null

addAll

public static void addAll(java.util.Collection collection,
                          java.util.Enumeration enumeration)
Adds all elements in the enumeration to the given collection.

Parameters:
collection - the collection to add to
enumeration - the enumeration of elements to add, may not be null
Throws:
java.lang.NullPointerException - if the collection or enumeration is null

addAll

public static void addAll(java.util.Collection collection,
                          java.lang.Object[] elements)
Adds all elements in the array to the given collection.

Parameters:
collection - the collection to add to
elements - the array of elements to add, may be null
Throws:
java.lang.NullPointerException - if the collection or array is null

index

public 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.

Parameters:
obj - the object to get an index of
Throws:
java.lang.IndexOutOfBoundsException
java.util.NoSuchElementException

index

public 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. The following checks are made:

Parameters:
obj - the object to get an index of
index - the index to get
Returns:
the object at the specified index
Throws:
java.lang.IndexOutOfBoundsException
java.util.NoSuchElementException

getIterator

public static java.util.Iterator getIterator(java.lang.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.


reverseArray

public static void reverseArray(java.lang.Object[] array)
Reverses the order of the given array


predicatedCollection

public static java.util.Collection predicatedCollection(java.util.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 null
predicate - the predicate for the collection, must not be null
Returns:
a predicated collection backed by the given collection
Throws:
java.lang.IllegalArgumentException - if the Collection is null


Copyright © 2001-2004 The Apache Software Foundation. All Rights Reserved.