org.apache.commons.collections
Class CollectionUtils

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

public class CollectionUtils
extends Object

A set of Collection related utility methods.

Version:
$Id: CollectionUtils.java,v 1.4 2001/05/22 15:53:06 jstrachan Exp $
Author:
Rodney Waldhoff

Constructor Summary
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 Collection disjunction(Collection a, Collection b)
          Returns a Collection containing the exclusive disjunction (symmetric difference) of the given Collections.
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 colleciton
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 Collection intersection(Collection a, Collection b)
          Returns a Collection containing the intersection of the given Collections.
static boolean isEqualCollection(Collection a, Collection b)
          Returns true iff the given Collections 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 select(Collection inputCollection, Predicate predicate)
          Selects all elements from inputCollection which match the given predicate into an output collection
static void select(Collection inputCollection, Predicate predicate, Collection outputCollection)
          Selects all elements from inputCollection which match the given predicate and adds them to outputCollection
static Collection subtract(Collection a, Collection b)
          Returns a Collection containing a - b.
static Collection union(Collection a, 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
 

Constructor Detail

CollectionUtils

public CollectionUtils()
Method Detail

addAll

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

addAll

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

addAll

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

cardinality

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

collect

public static Collection collect(Collection inputCollection,
                                 Transformer transformer)
Transforms all elements from inputCollection with the given transformer and adds them to the outputCollection

collect

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
Returns:
the outputCollection

collect

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

collect

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
Returns:
the outputCollection

disjunction

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


find

public static Object find(Collection collection,
                          Predicate predicate)
Finds the first element in the given collection which matches the given predicate
Returns:
the first element of the collection which matches the predicate or null if none could be found

forAllDo

public static void forAllDo(Collection collection,
                            Closure closure)
Executes the given closure on each element in the colleciton

getCardinalityMap

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

intersection

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

isEqualCollection

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


isProperSubCollection

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(java.util.Collection, java.util.Collection), Collection.containsAll(java.util.Collection)

isSubCollection

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(java.util.Collection, java.util.Collection), Collection.containsAll(java.util.Collection)

select

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

select

public static void select(Collection inputCollection,
                          Predicate predicate,
                          Collection outputCollection)
Selects all elements from inputCollection which match the given predicate and adds them to outputCollection

subtract

public static Collection subtract(Collection a,
                                  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)

union

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


Copyright © 2001 Apache Software Foundation. Documenation generated July 14 2001.