From 6f42ddf1baa4c6f5f385684dd03d57eefce38406 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Wed, 17 Apr 2013 19:32:34 +0000 Subject: [PATCH] [COLLECTIONS-429] Improved CollectionUtils#containsAll with the version from the patch, added more javadoc with information on the runtime/space trade-off. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1469039 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 6 +++ .../commons/collections4/CollectionUtils.java | 50 ++++++++++--------- 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1bfcf6299..c71191ca6 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,12 @@ + + Added method "CollectionUtils#containsAll(Collection, Collection)" with guaranteed + runtime complexity of O(n + m) and space complexity of O(n). This method may yield much + better performance than "Collection#containsAll(Collection)" depending on the use-case and + type of collection used. + Added serialization support for "TreeBidiMap". diff --git a/src/main/java/org/apache/commons/collections4/CollectionUtils.java b/src/main/java/org/apache/commons/collections4/CollectionUtils.java index b3a5a12cc..2faf5fa09 100644 --- a/src/main/java/org/apache/commons/collections4/CollectionUtils.java +++ b/src/main/java/org/apache/commons/collections4/CollectionUtils.java @@ -105,23 +105,6 @@ public class CollectionUtils { return getFreq(obj, cardinalityB); } - /** - * Returns the number of unique elements in collection A. - * @return the number of unique elements in collection A - */ - @SuppressWarnings("unused") - public int sizeA() { - return cardinalityA.size(); - } - - /** - * Returns the number of unique elements in collection A. - * @return the number of unique elements in collection A - */ - public int sizeB() { - return cardinalityB.size(); - } - private final int getFreq(final Object obj, final Map freqMap) { final Integer count = freqMap.get(obj); if (count != null) { @@ -366,9 +349,10 @@ public class CollectionUtils { * will be returned. *

* This method is intended as a replacement for {@link Collection#containsAll(Collection)} - * with a guaranteed runtime complexity of {@code O(n)}. Depending on the type of + * with a guaranteed runtime complexity of {@code O(n + m)}. Depending on the type of * {@link Collection} provided, this method will be much faster than calling - * {@link Collection#containsAll(Collection)} instead. + * {@link Collection#containsAll(Collection)} instead, though this will come at the + * cost of an additional space complexity O(n). * * @param coll1 the first collection, must not be null * @param coll2 the second collection, must not be null @@ -380,12 +364,30 @@ public class CollectionUtils { if (coll2.isEmpty()) { return true; } else { - final SetOperationCardinalityHelper helper = - new SetOperationCardinalityHelper(coll1, coll2); - for (final Object obj : helper) { - helper.setCardinality(obj, helper.min(obj)); + final Iterator it = coll1.iterator(); + final Set elementsAlreadySeen = new HashSet(); + for (final Object nextElement : coll2) { + if (elementsAlreadySeen.contains(nextElement)) { + continue; + } + + boolean foundCurrentElement = false; + while (it.hasNext()) { + final Object p = it.next(); + elementsAlreadySeen.add(p); + if (nextElement == null ? p == null : nextElement.equals(p)) { + foundCurrentElement = true; + break; + } + } + + if (foundCurrentElement) { + continue; + } else { + return false; + } } - return helper.list().size() == helper.sizeB(); + return true; } }