Small performance improvement

bug 22973, from Janek Bogucki
Method and Javadoc improvement


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131148 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-09-09 21:25:18 +00:00
parent b7db6423cb
commit 90e985280f

View File

@ -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 * 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. * A set of {@link Collection} related utility methods.
* *
* @since Commons Collections 1.0 * @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 Rodney Waldhoff
* @author Paul Jack * @author Paul Jack
@ -94,6 +94,9 @@ import org.apache.commons.collections.iterators.EnumerationIterator;
*/ */
public class CollectionUtils { public class CollectionUtils {
/** Constant to avoid repeated object creation */
private static Integer INTEGER_ONE = new Integer(1);
/** /**
* An empty unmodifiable collection. * An empty unmodifiable collection.
* The JDK provides empty Set and List implementations which could be used for * 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 <tt><i>a</i> - <i>b</i></tt>. * Returns a new {@link Collection} containing <tt><i>a</i> - <i>b</i></tt>.
* The cardinality of each element <i>e</i> in the returned {@link Collection} * The cardinality of each element <i>e</i> in the returned {@link Collection}
* will be the cardinality of <i>e</i> in <i>a</i> minus the cardinality * will be the cardinality of <i>e</i> in <i>a</i> minus the cardinality
* of <i>e</i> in <i>b</i>, or zero, whichever is greater. * of <i>e</i> in <i>b</i>, 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 * @see Collection#removeAll
*/ */
public static Collection subtract(final Collection a, final Collection b) { public static Collection subtract(final Collection a, final Collection b) {
ArrayList list = new ArrayList( a ); ArrayList list = new ArrayList( a );
Iterator it = b.iterator(); for (Iterator it = b.iterator(); it.hasNext();) {
while(it.hasNext()) {
list.remove(it.next()); list.remove(it.next());
} }
return list; return list;
} }
/** /**
* Returns <code>true</code> iff some element of <i>a</i> * Returns <code>true</code> iff at least one element is in both collections.
* is also an element of <i>b</i> (or, equivalently, if * <p>
* some element of <i>b</i> is also an element of <i>a</i>). * In other words, this method returns <code>true</code> iff the
* In other words, this method returns <code>true</code> * {@link #intersection} of <i>coll1</i> and <i>coll2</i> is not empty.
* iff the {@link #intersection} of <i>a</i> and <i>b</i> *
* is not empty. * @param coll1 the first collection, must not be null
* @param coll2 the first collection, must not be null
* @return <code>true</code> iff the intersection of the collections is non-empty
* @since 2.1 * @since 2.1
* @param a a non-<code>null</code> Collection
* @param b a non-<code>null</code> Collection
* @return <code>true</code> iff the intersection of <i>a</i> and <i>b</i> is non-empty
* @see #intersection * @see #intersection
*/ */
public static boolean containsAny(final Collection a, final Collection b) { public static boolean containsAny(final Collection coll1, final Collection coll2) {
// TO DO: we may be able to optimize this by ensuring either a or b if (coll1.size() > coll2.size()) {
// is the larger of the two Collections, but I'm not sure which. for (Iterator it = coll1.iterator(); it.hasNext();) {
for(Iterator iter = a.iterator(); iter.hasNext();) { if (coll2.contains(it.next())) {
if(b.contains(iter.next())) { return true;
return true; }
}
} else {
for (Iterator it = coll2.iterator(); it.hasNext();) {
if (coll1.contains(it.next())) {
return true;
}
} }
} }
return false; return false;
} }
/** /**
* Returns a {@link Map} mapping each unique element in * Returns a {@link Map} mapping each unique element in the given
* the given {@link Collection} to an {@link Integer} * {@link Collection} to an {@link Integer} representing the number
* representing the number of occurences of that element * of occurences of that element in the {@link Collection}.
* in the {@link Collection}. * <p>
* An entry that maps to <tt>null</tt> indicates that the * Only those elements present in the collection will appear as
* element does not appear in the given {@link Collection}. * 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) { public static Map getCardinalityMap(final Collection coll) {
HashMap count = new HashMap(); Map count = new HashMap();
Iterator it = col.iterator(); for (Iterator it = coll.iterator(); it.hasNext();) {
while(it.hasNext()) {
Object obj = it.next(); Object obj = it.next();
Integer c = (Integer)(count.get(obj)); Integer c = (Integer) (count.get(obj));
if(null == c) { if (c == null) {
count.put(obj,new Integer(1)); count.put(obj,INTEGER_ONE);
} else { } else {
count.put(obj,new Integer(c.intValue() + 1)); count.put(obj,new Integer(c.intValue() + 1));
} }