diff --git a/src/changes/changes.xml b/src/changes/changes.xml index a95400ec7b..aa9767bbef 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -40,6 +40,8 @@ Add CollectionUtils.duplicateList(Collection). Add CollectionUtils.duplicateSet(Collection). Add CollectionUtils.duplicateSequencedSet(Collection). + Add HashBag.HashBag(Iterable). + Add TreeBag.TreeBag(Iterable). Update bloom filter documentation #508. Bump commons-codec:commons-codec from 1.17.0 to 1.17.1 #514. diff --git a/src/main/java/org/apache/commons/collections4/bag/AbstractMapBag.java b/src/main/java/org/apache/commons/collections4/bag/AbstractMapBag.java index f4be8902a0..d3fa5e6e1e 100644 --- a/src/main/java/org/apache/commons/collections4/bag/AbstractMapBag.java +++ b/src/main/java/org/apache/commons/collections4/bag/AbstractMapBag.java @@ -25,6 +25,7 @@ import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; import org.apache.commons.collections4.Bag; @@ -165,7 +166,19 @@ protected AbstractMapBag() { * @param map the map to assign */ protected AbstractMapBag(final Map map) { - this.map = map; + this.map = Objects.requireNonNull(map, "map"); + } + + /** + * Constructs a new instance that assigns the specified Map as the backing store. The map + * must be empty and non-null. The bag is filled from the iterable elements. + * + * @param map the map to assign. + * @param iterable The bag is filled from these iterable elements. + */ + protected AbstractMapBag(final Map map, final Iterable iterable) { + this(map); + iterable.forEach(this::add); } /** diff --git a/src/main/java/org/apache/commons/collections4/bag/HashBag.java b/src/main/java/org/apache/commons/collections4/bag/HashBag.java index 1335669aa1..6b33ab8b7f 100644 --- a/src/main/java/org/apache/commons/collections4/bag/HashBag.java +++ b/src/main/java/org/apache/commons/collections4/bag/HashBag.java @@ -50,13 +50,23 @@ public HashBag() { } /** - * Constructs a bag containing all the members of the given collection. + * Constructs a bag containing all the members of the given Collection. * - * @param coll a collection to copy into this bag + * @param collection a collection to copy into this bag. */ - public HashBag(final Collection coll) { + public HashBag(final Collection collection) { this(); - addAll(coll); + addAll(collection); + } + + /** + * Constructs a bag containing all the members of the given Iterable. + * + * @param iterable an iterable to copy into this bag. + * @since 4.5.0-M3 + */ + public HashBag(final Iterable iterable) { + super(new HashMap<>(), iterable); } /** diff --git a/src/main/java/org/apache/commons/collections4/bag/TreeBag.java b/src/main/java/org/apache/commons/collections4/bag/TreeBag.java index fa10f94db6..14f7a6802d 100644 --- a/src/main/java/org/apache/commons/collections4/bag/TreeBag.java +++ b/src/main/java/org/apache/commons/collections4/bag/TreeBag.java @@ -67,6 +67,16 @@ public TreeBag(final Collection coll) { addAll(coll); } + /** + * Constructs a bag containing all the members of the given Iterable. + * + * @param iterable an iterable to copy into this bag. + * @since 4.5.0-M3 + */ + public TreeBag(final Iterable iterable) { + super(new TreeMap<>(), iterable); + } + /** * Constructs an empty bag that maintains order on its unique representative * members according to the given {@link Comparator}. diff --git a/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java b/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java index ac80948e2e..b2905b98f1 100644 --- a/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java +++ b/src/test/java/org/apache/commons/collections4/multimap/AbstractMultiValuedMapTest.java @@ -931,7 +931,7 @@ public void testKeysBagContainsAll() { } @Test - public void testKeysBagIterator() { + public void testKeysBagIterator1() { final MultiValuedMap map = makeFullMap(); final Collection col = new ArrayList<>(map.keys()); final Bag bag = new HashBag<>(col); @@ -942,6 +942,18 @@ public void testKeysBagIterator() { assertEquals(getSampleTotalValueCount(), bag.size()); } + @Test + public void testKeysBagIterator2() { + final MultiValuedMap map = makeFullMap(); + final Iterable iterable = new ArrayList<>(map.keys()); + final Bag bag = new HashBag<>(iterable); + final int maxK = getSampleKeySize(); + for (int k = 0; k < maxK; k++) { + assertEquals(getSampleCountPerKey(), bag.getCount(makeKey(k))); + } + assertEquals(getSampleTotalValueCount(), bag.size()); + } + @Test public void testKeySetSize() { final MultiValuedMap map = makeFullMap();