diff --git a/src/java/org/apache/commons/collections/CollectionUtils.java b/src/java/org/apache/commons/collections/CollectionUtils.java
index 99d5d14db..a4599444b 100644
--- a/src/java/org/apache/commons/collections/CollectionUtils.java
+++ b/src/java/org/apache/commons/collections/CollectionUtils.java
@@ -1,5 +1,5 @@
/*
- * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.38 2003/09/07 15:09:34 psteitz Exp $
+ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/CollectionUtils.java,v 1.39 2003/09/09 21:25:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@@ -81,7 +81,7 @@ import org.apache.commons.collections.iterators.EnumerationIterator;
* A set of {@link Collection} related utility methods.
*
* @since Commons Collections 1.0
- * @version $Revision: 1.38 $ $Date: 2003/09/07 15:09:34 $
+ * @version $Revision: 1.39 $ $Date: 2003/09/09 21:25:18 $
*
* @author Rodney Waldhoff
* @author Paul Jack
@@ -94,6 +94,9 @@ import org.apache.commons.collections.iterators.EnumerationIterator;
*/
public class CollectionUtils {
+ /** Constant to avoid repeated object creation */
+ private static Integer INTEGER_ONE = new Integer(1);
+
/**
* An empty unmodifiable collection.
* The JDK provides empty Set and List implementations which could be used for
@@ -191,62 +194,71 @@ public class CollectionUtils {
}
/**
- * Returns a {@link Collection} containing a - b.
+ * Returns a new {@link Collection} containing a - b.
* The cardinality of each element e in the returned {@link Collection}
* will be the cardinality of e in a minus the cardinality
* of e in b, or zero, whichever is greater.
*
+ * @param a the collection to subtract from, must not be null
+ * @param b the collection to subtract, must not be null
+ * @return a new collection with the results
* @see Collection#removeAll
*/
public static Collection subtract(final Collection a, final Collection b) {
ArrayList list = new ArrayList( a );
- Iterator it = b.iterator();
- while(it.hasNext()) {
+ for (Iterator it = b.iterator(); it.hasNext();) {
list.remove(it.next());
}
return list;
}
/**
- * 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 {@link #intersection} of a and b
- * is not empty.
+ * Returns true
iff at least one element is in both collections.
+ *
+ * In other words, this method returns true
iff the
+ * {@link #intersection} of coll1 and coll2 is not empty.
+ *
+ * @param coll1 the first collection, must not be null
+ * @param coll2 the first collection, must not be null
+ * @return true
iff the intersection of the collections is non-empty
* @since 2.1
- * @param a a non-null
Collection
- * @param b a non-null
Collection
- * @return true
iff the intersection of a and b is non-empty
* @see #intersection
*/
- public static boolean containsAny(final Collection a, final Collection b) {
- // TO DO: we may be able to optimize this by ensuring either a or b
- // is the larger of the two Collections, but I'm not sure which.
- for(Iterator iter = a.iterator(); iter.hasNext();) {
- if(b.contains(iter.next())) {
- return true;
+ public static boolean containsAny(final Collection coll1, final Collection coll2) {
+ if (coll1.size() > coll2.size()) {
+ for (Iterator it = coll1.iterator(); it.hasNext();) {
+ if (coll2.contains(it.next())) {
+ return true;
+ }
+ }
+ } else {
+ for (Iterator it = coll2.iterator(); it.hasNext();) {
+ if (coll1.contains(it.next())) {
+ return true;
+ }
}
}
return false;
}
/**
- * Returns a {@link Map} mapping each unique element in
- * the given {@link Collection} to an {@link Integer}
- * representing the number of occurences of that element
- * in the {@link Collection}.
- * An entry that maps to null indicates that the
- * element does not appear in the given {@link Collection}.
+ * Returns a {@link Map} mapping each unique element in the given
+ * {@link Collection} to an {@link Integer} representing the number
+ * of occurences of that element in the {@link Collection}.
+ *
+ * Only those elements present in the collection will appear as + * keys in the map. + * + * @param coll the collection to get the cardinality map for + * @return the populated cardinality map */ - public static Map getCardinalityMap(final Collection col) { - HashMap count = new HashMap(); - Iterator it = col.iterator(); - while(it.hasNext()) { + public static Map getCardinalityMap(final Collection coll) { + Map count = new HashMap(); + for (Iterator it = coll.iterator(); it.hasNext();) { Object obj = it.next(); - Integer c = (Integer)(count.get(obj)); - if(null == c) { - count.put(obj,new Integer(1)); + Integer c = (Integer) (count.get(obj)); + if (c == null) { + count.put(obj,INTEGER_ONE); } else { count.put(obj,new Integer(c.intValue() + 1)); }