org.apache.commons.collections
Class CollectionUtils

java.lang.Object
  extended by org.apache.commons.collections.CollectionUtils

public class CollectionUtils
extends Object

Provides utility methods and decorators for Collection instances.

Since:
Commons Collections 1.0
Version:
$Revision: 1.1.1.1 $ $Date: 2005/05/23 04:32:47 $
Author:
Rodney Waldhoff, Paul Jack, Stephen Colebourne, Steve Downey, Herve Quiroz, Peter KoBek, Matthew Hawthorne, Janek Bogucki, Phil Steitz, Steven Melzer, Matt Hall, John Watkinson, Jon Schewe

Field Summary
static Collection EMPTY_COLLECTION
          An empty unmodifiable collection.
 
Constructor Summary
CollectionUtils()
          CollectionUtils should not normally be instantiated.
 
Method Summary
static
<E> void
addAll(Collection<E> collection, E[] elements)
          Adds all elements in the array to the given collection.
static
<E> void
addAll(Collection<E> collection, Enumeration<E> enumeration)
          Adds all elements in the enumeration to the given collection.
static
<E> void
addAll(Collection<E> collection, Iterator<E> iterator)
          Adds all elements in the iteration to the given collection.
static
<E> int
cardinality(E obj, Collection<E> coll)
          Returns the number of occurrences of obj in coll.
static
<I,O> Collection<O>
collect(Collection<I> inputCollection, Transformer<I,O> transformer)
          Returns a new Collection consisting of the elements of inputCollection transformed by the given transformer.
static
<I,O> Collection<O>
collect(Collection<I> inputCollection, Transformer<I,O> transformer, Collection<O> outputCollection)
          Transforms all elements from inputCollection with the given transformer and adds them to the outputCollection.
static
<I,O> Collection<O>
collect(Iterator<I> inputIterator, Transformer<I,O> transformer)
          Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection.
static
<I,O> Collection<O>
collect(Iterator<I> inputIterator, Transformer<I,O> transformer, Collection<O> outputCollection)
          Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection.
static
<E> boolean
containsAny(Collection<E> coll1, Collection<E> coll2)
          Returns true iff at least one element is in both collections.
static
<E> int
countMatches(Collection<E> inputCollection, Predicate<E> predicate)
          Counts the number of elements in the input collection that match the predicate.
static
<E> Collection<E>
disjunction(Collection<E> a, Collection<E> b)
          Returns a Collection containing the exclusive disjunction (symmetric difference) of the given Collections.
static
<E> boolean
exists(Collection<E> collection, Predicate<E> predicate)
          Answers true if a predicate is true for at least one element of a collection.
static
<E> void
filter(Collection<E> collection, Predicate<E> predicate)
          Filter the collection by applying a Predicate to each element.
static
<E> E
find(Collection<E> collection, Predicate<E> predicate)
          Finds the first element in the given collection which matches the given predicate.
static
<E> void
forAllDo(Collection<E> collection, Closure<E> closure)
          Executes the given closure on each element in the collection.
static Object get(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
<E> Map<E,Integer>
getCardinalityMap(Collection<E> 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 Object index(Object obj, int idx)
          Deprecated. use get(Object, int) instead. Will be removed in v4.0
static Object index(Object obj, Object index)
          Deprecated. use get(Object, int) instead. Will be removed in v4.0
static
<E> Collection<E>
intersection(Collection<E> a, Collection<E> b)
          Returns a Collection containing the intersection of the given Collections.
static
<E> boolean
isEqualCollection(Collection<E> a, Collection<E> b)
          Returns true iff the given Collections contain exactly the same elements with exactly the same cardinalities.
static boolean isFull(Collection coll)
          Returns true if no more elements can be added to the Collection.
static
<E> boolean
isProperSubCollection(Collection<E> a, Collection<E> 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
<E> boolean
isSubCollection(Collection<E> a, Collection<E> 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 int maxSize(Collection coll)
          Get the maximum number of elements that the Collection can contain.
static
<E> Collection<E>
predicatedCollection(Collection<E> collection, Predicate<E> predicate)
          Returns a predicated (validating) collection backed by the given collection.
static void reverseArray(Object[] array)
          Reverses the order of the given array.
static
<E> Collection<E>
select(Collection<E> inputCollection, Predicate<E> predicate)
          Selects all elements from input collection which match the given predicate into an output collection.
static
<E> void
select(Collection<E> inputCollection, Predicate<E> predicate, Collection<E> outputCollection)
          Selects all elements from input collection which match the given predicate and adds them to outputCollection.
static
<E> Collection<E>
selectRejected(Collection<E> inputCollection, Predicate<E> predicate)
          Selects all elements from inputCollection which don't match the given predicate into an output collection.
static
<E> void
selectRejected(Collection<E> inputCollection, Predicate<E> predicate, Collection<E> outputCollection)
          Selects all elements from inputCollection which don't match the given predicate and adds them to outputCollection.
static int size(Object object)
          Gets the size of the collection/iterator specified.
static
<E> Collection<E>
subtract(Collection<E> a, Collection<E> b)
          Returns a new Collection containing a - b.
static
<E> Collection<E>
synchronizedCollection(Collection<E> collection)
          Returns a synchronized collection backed by the given collection.
static
<E> void
transform(Collection<E> collection, Transformer<E,E> transformer)
          Transform the collection by applying a Transformer to each element.
static
<I,O> Collection<O>
transformedCollection(Collection<I> collection, Transformer<I,O> transformer)
          Returns a transformed bag backed by the given collection.
static
<E> Collection<E>
typedCollection(Collection<E> collection, Class<E> type)
          Deprecated. Obsoleted by Java 1.5 Generics.
static
<E> Collection<E>
union(Collection<E> a, Collection<E> b)
          Returns a Collection containing the union of the given Collections.
static
<E> Collection<E>
unmodifiableCollection(Collection<E> collection)
          Returns an unmodifiable collection backed by the given collection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_COLLECTION

public static final Collection EMPTY_COLLECTION
An empty unmodifiable collection. The JDK provides empty Set and List implementations which could be used for this purpose. However they could be cast to Set or List which might be undesirable. This implementation only implements Collection.

Constructor Detail

CollectionUtils

public CollectionUtils()
CollectionUtils should not normally be instantiated.

Method Detail

union

public static <E> Collection<E> union(Collection<E> a,
                                      Collection<E> 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.

Parameters:
a - the first collection, must not be null
b - the second collection, must not be null
Returns:
the union of the two collections
See Also:
Collection.addAll(java.util.Collection)

intersection

public static <E> Collection<E> intersection(Collection<E> a,
                                             Collection<E> 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.

Parameters:
a - the first collection, must not be null
b - the second collection, must not be null
Returns:
the intersection of the two collections
See Also:
Collection.retainAll(java.util.Collection), containsAny(java.util.Collection, java.util.Collection)

disjunction

public static <E> Collection<E> disjunction(Collection<E> a,
                                            Collection<E> 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)).

Parameters:
a - the first collection, must not be null
b - the second collection, must not be null
Returns:
the symmetric difference of the two collections

subtract

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

Parameters:
a - the collection to subtract from, must not be null
b - the collection to subtract, must not be null
Returns:
a new collection with the results
See Also:
Collection.removeAll(java.util.Collection)

containsAny

public static <E> boolean containsAny(Collection<E> coll1,
                                      Collection<E> coll2)
Returns true iff at least one element is in both collections.

In other words, this method returns true iff the intersection(java.util.Collection, java.util.Collection) of coll1 and coll2 is not empty.

Parameters:
coll1 - the first collection, must not be null
coll2 - the first collection, must not be null
Returns:
true iff the intersection of the collections is non-empty
Since:
2.1
See Also:
intersection(java.util.Collection, java.util.Collection)

getCardinalityMap

public static <E> Map<E,Integer> getCardinalityMap(Collection<E> 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.

Only those elements present in the collection will appear as keys in the map.

Parameters:
coll - the collection to get the cardinality map for, must not be null
Returns:
the populated cardinality map

isSubCollection

public static <E> boolean isSubCollection(Collection<E> a,
                                          Collection<E> 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.

Parameters:
a - the first (sub?) collection, must not be null
b - the second (super?) collection, must not be null
Returns:
true iff a is a sub-collection of b
See Also:
isProperSubCollection(java.util.Collection, java.util.Collection), Collection.containsAll(java.util.Collection)

isProperSubCollection

public static <E> boolean isProperSubCollection(Collection<E> a,
                                                Collection<E> 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.

The implementation assumes

Parameters:
a - the first (sub?) collection, must not be null
b - the second (super?) collection, must not be null
Returns:
true iff a is a proper sub-collection of b
See Also:
isSubCollection(java.util.Collection, java.util.Collection), Collection.containsAll(java.util.Collection)

isEqualCollection

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

Parameters:
a - the first collection, must not be null
b - the second collection, must not be null
Returns:
true iff the collections contain the same elements with the same cardinalities.

cardinality

public static <E> int cardinality(E obj,
                                  Collection<E> coll)
Returns the number of occurrences of obj in coll.

Parameters:
obj - the object to find the cardinality of
coll - the collection to search
Returns:
the the number of occurrences of obj in coll

find

public static <E> E find(Collection<E> collection,
                         Predicate<E> predicate)
Finds the first element in the given collection which matches the given predicate.

If the input collection or predicate is null, or no element of the collection matches the predicate, 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 <E> void forAllDo(Collection<E> collection,
                                Closure<E> closure)
Executes the given closure on each element in the collection.

If the input collection or closure 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 be null

filter

public static <E> void filter(Collection<E> collection,
                              Predicate<E> 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 <E> void transform(Collection<E> collection,
                                 Transformer<E,E> 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, 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.

Parameters:
collection - the collection to get the input from, may be null
transformer - the transformer to perform, may be null

countMatches

public static <E> int countMatches(Collection<E> inputCollection,
                                   Predicate<E> predicate)
Counts the number of elements in the input collection that match the predicate.

A null collection or predicate matches no elements.

Parameters:
inputCollection - the collection to get the input from, may be null
predicate - the predicate to use, may be null
Returns:
the number of matches for the predicate in the collection

exists

public static <E> boolean exists(Collection<E> collection,
                                 Predicate<E> predicate)
Answers true if a predicate is true for at least one element of a collection.

A null collection or predicate returns false.

Parameters:
collection - the collection to get the input from, may be null
predicate - the predicate to use, may be null
Returns:
true if at least one element of the collection matches the predicate

select

public static <E> Collection<E> select(Collection<E> inputCollection,
                                       Predicate<E> predicate)
Selects all elements from input collection which match the given predicate into an output collection.

A null predicate matches no elements.

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:
NullPointerException - if the input collection is null

select

public static <E> void select(Collection<E> inputCollection,
                              Predicate<E> predicate,
                              Collection<E> 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

selectRejected

public static <E> Collection<E> selectRejected(Collection<E> inputCollection,
                                               Predicate<E> predicate)
Selects all elements from inputCollection which don't match the given predicate into an output collection.

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

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

selectRejected

public static <E> void selectRejected(Collection<E> inputCollection,
                                      Predicate<E> predicate,
                                      Collection<E> outputCollection)
Selects all elements from inputCollection which don't match the given predicate and adds them to outputCollection.

If the input predicate is null, no elements are added to outputCollection.

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

collect

public static <I,O> Collection<O> collect(Collection<I> inputCollection,
                                          Transformer<I,O> transformer)
Returns a new Collection consisting of the elements of inputCollection transformed by the given transformer.

If the input transformer 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:
NullPointerException - if the input collection is null

collect

public static <I,O> Collection<O> collect(Iterator<I> inputIterator,
                                          Transformer<I,O> transformer)
Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection.

If the input iterator or transformer 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 <I,O> Collection<O> collect(Collection<I> inputCollection,
                                          Transformer<I,O> transformer,
                                          Collection<O> outputCollection)
Transforms all elements from inputCollection with the given transformer and adds them to the outputCollection.

If the input collection or transformer 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:
NullPointerException - if the output collection is null

collect

public static <I,O> Collection<O> collect(Iterator<I> inputIterator,
                                          Transformer<I,O> transformer,
                                          Collection<O> outputCollection)
Transforms all elements from the inputIterator with the given transformer and adds them to the outputCollection.

If the input iterator or transformer 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:
NullPointerException - if the output collection is null

addAll

public static <E> void addAll(Collection<E> collection,
                              Iterator<E> 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:
NullPointerException - if the collection or iterator is null

addAll

public static <E> void addAll(Collection<E> collection,
                              Enumeration<E> 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:
NullPointerException - if the collection or enumeration is null

addAll

public static <E> void addAll(Collection<E> collection,
                              E[] elements)
Adds all elements in the array to the given collection.

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

index

public static Object index(Object obj,
                           int idx)
Deprecated. use get(Object, int) instead. Will be removed in v4.0

Given an Object, and an index, returns the nth value in the object.

Parameters:
obj - the object to get an index of, may be null
idx - the index to get
Throws:
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException

index

public static Object index(Object obj,
                           Object index)
Deprecated. use get(Object, int) instead. Will be removed in v4.0

Given an Object, and a key (index), returns the 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:
IndexOutOfBoundsException
ArrayIndexOutOfBoundsException

get

public static Object get(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.

The supported types, and associated semantics are:

Parameters:
object - the object to get a value from
index - the index to get
Returns:
the object at the specified index
Throws:
IndexOutOfBoundsException - if the index is invalid
IllegalArgumentException - if the object type is invalid

size

public static int size(Object object)
Gets the size of the collection/iterator specified.

This method can handles objects as follows

Parameters:
object - the object to get the size of
Returns:
the size of the specified collection
Throws:
IllegalArgumentException - thrown if object is not recognised or null
Since:
Commons Collections 3.1

reverseArray

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

Parameters:
array - the array to reverse

isFull

public static boolean isFull(Collection coll)
Returns true if no more elements can be added to the Collection.

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.

Parameters:
coll - the collection to check
Returns:
true if the BoundedCollection is full
Throws:
NullPointerException - if the collection is null

maxSize

public static int maxSize(Collection coll)
Get the maximum number of elements that the Collection can contain.

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.

Parameters:
coll - the collection to check
Returns:
the maximum size of the BoundedCollection, -1 if no maximum size
Throws:
NullPointerException - if the collection is null

synchronizedCollection

public static <E> Collection<E> synchronizedCollection(Collection<E> collection)
Returns a synchronized collection backed by the given collection.

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

Parameters:
collection - the collection to synchronize, must not be null
Returns:
a synchronized collection backed by the given collection
Throws:
IllegalArgumentException - if the collection is null

unmodifiableCollection

public static <E> Collection<E> unmodifiableCollection(Collection<E> collection)
Returns an unmodifiable collection backed by the given collection.

This method uses the implementation in the decorators subpackage.

Parameters:
collection - the collection to make unmodifiable, must not be null
Returns:
an unmodifiable collection backed by the given collection
Throws:
IllegalArgumentException - if the collection is null

predicatedCollection

public static <E> Collection<E> predicatedCollection(Collection<E> collection,
                                                     Predicate<E> predicate)
Returns a predicated (validating) collection backed by the given collection.

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.

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:
IllegalArgumentException - if the Collection is null

typedCollection

public static <E> Collection<E> typedCollection(Collection<E> collection,
                                                Class<E> type)
Deprecated. Obsoleted by Java 1.5 Generics.

Returns a typed collection backed by the given collection.

Only objects of the specified type can be added to the collection.

Parameters:
collection - the collection to limit to a specific type, must not be null
type - the type of objects which may be added to the collection
Returns:
a typed collection backed by the specified collection

transformedCollection

public static <I,O> Collection<O> transformedCollection(Collection<I> collection,
                                                        Transformer<I,O> transformer)
Returns a transformed bag backed by the given collection.

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.

Parameters:
collection - the collection to predicate, must not be null
transformer - the transformer for the collection, must not be null
Returns:
a transformed collection backed by the given collection
Throws:
IllegalArgumentException - if the Collection or Transformer is null


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.