This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git

commit bf840192b20f1d3240e5b36265f5c799ae0152ce
Author: Gary D. Gregory <[email protected]>
AuthorDate: Sun Mar 16 15:18:30 2025 -0400

    Add IteratorUtils.toSet(Iterator, int)
    
    Add IteratorUtils.toSet(Iterator)
---
 src/changes/changes.xml                            |  2 +
 .../apache/commons/collections4/IteratorUtils.java | 56 +++++++++++++++++++---
 .../commons/collections4/IteratorUtilsTest.java    | 13 +++++
 3 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index fea90ec01..05df0d206 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -27,6 +27,8 @@
     <action type="fix" dev="ggregory" due-to="Gary Gregory">Refactor 
AbstractPropertiesFactory.load(File) to use NIO.</action>
     <action type="fix" dev="ggregory" due-to="Gary Gregory">Refactor 
AbstractPropertiesFactory.load(String) to use NIO.</action>
     <!-- ADD -->
+    <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
IteratorUtils.toSet(Iterator).</action>
+    <action type="add" dev="ggregory" due-to="Gary Gregory">Add 
IteratorUtils.toSet(Iterator, int).</action>
     <!-- UPDATE -->
     <action type="update" dev="ggregory" due-to="Gary Gregory">Bump 
commons-codec:commons-codec from 1.17.1 to 1.18.0 #591.</action>
     <action type="update" dev="ggregory" due-to="Gary Gregory, 
Dependabot">Bump org.apache.commons:commons-parent from 78 to 81 #2296.</action>
diff --git a/src/main/java/org/apache/commons/collections4/IteratorUtils.java 
b/src/main/java/org/apache/commons/collections4/IteratorUtils.java
index c8b246b3d..02e8be85f 100644
--- a/src/main/java/org/apache/commons/collections4/IteratorUtils.java
+++ b/src/main/java/org/apache/commons/collections4/IteratorUtils.java
@@ -23,11 +23,13 @@ import java.util.Collection;
 import java.util.Comparator;
 import java.util.Dictionary;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 import java.util.Spliterator;
 import java.util.Spliterators;
 import java.util.function.IntFunction;
@@ -116,6 +118,14 @@ public class IteratorUtils {
      */
     private static final String DEFAULT_TOSTRING_DELIMITER = ", ";
 
+    private static <E, C extends Collection<E>> C addAll(final Iterator<? 
extends E> iterator, final C list) {
+        Objects.requireNonNull(iterator, "iterator");
+        while (iterator.hasNext()) {
+            list.add(iterator.next());
+        }
+        return list;
+    }
+
     /**
      * Gets an iterator over an object array.
      *
@@ -1325,15 +1335,10 @@ public class IteratorUtils {
      * @throws IllegalArgumentException if the size is less than 1
      */
     public static <E> List<E> toList(final Iterator<? extends E> iterator, 
final int estimatedSize) {
-        Objects.requireNonNull(iterator, "iterator");
         if (estimatedSize < 1) {
             throw new IllegalArgumentException("Estimated size must be greater 
than 0");
         }
-        final List<E> list = new ArrayList<>(estimatedSize);
-        while (iterator.hasNext()) {
-            list.add(iterator.next());
-        }
-        return list;
+        return addAll(iterator, new ArrayList<>(estimatedSize));
     }
 
     /**
@@ -1353,6 +1358,45 @@ public class IteratorUtils {
         return new ListIteratorWrapper<>(iterator);
     }
 
+    /**
+     * Gets a set based on an iterator.
+     * <p>
+     * As the wrapped Iterator is traversed, a HashSet of its values is
+     * created. At the end, the set is returned.
+     * </p>
+     *
+     * @param <E> the element type
+     * @param iterator  the iterator to use, not null
+     * @return a set of the iterator contents
+     * @throws NullPointerException if iterator parameter is null
+     * @since 4.5.0-M4
+     */
+    public static <E> Set<E> toSet(final Iterator<? extends E> iterator) {
+        return toSet(iterator, 10);
+    }
+
+    /**
+     * Gets a set based on an iterator.
+     * <p>
+     * As the wrapped Iterator is traversed, a HashSet of its values is
+     * created. At the end, the set is returned.
+     * </p>
+     *
+     * @param <E> the element type
+     * @param iterator  the iterator to use, not null
+     * @param estimatedSize  the initial size of the HashSet
+     * @return a list of the iterator contents
+     * @throws NullPointerException if iterator parameter is null
+     * @throws IllegalArgumentException if the size is less than 1
+     * @since 4.5.0-M4
+     */
+    public static <E> Set<E> toSet(final Iterator<? extends E> iterator, final 
int estimatedSize) {
+        if (estimatedSize < 1) {
+            throw new IllegalArgumentException("Estimated size must be greater 
than 0");
+        }
+        return addAll(iterator, new HashSet<>(estimatedSize));
+    }
+
     /**
      * Returns a string representation of the elements of the specified 
iterator.
      * <p>
diff --git 
a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java 
b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java
index 167b3e5e3..b225cb873 100644
--- a/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/IteratorUtilsTest.java
@@ -35,6 +35,7 @@ import java.util.Comparator;
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -1023,6 +1024,18 @@ public class IteratorUtilsTest {
         );
     }
 
+    @Test
+    public void testToSet() {
+        final Set<Object> set = new HashSet<>();
+        set.add(Integer.valueOf(1));
+        set.add("Two");
+        set.add(null);
+        final Set<Object> result = IteratorUtils.toSet(set.iterator());
+        assertEquals(set, result);
+        assertThrows(NullPointerException.class, () -> 
IteratorUtils.toSet(null, 10), "Expecting NullPointerException");
+        assertThrows(IllegalArgumentException.class, () -> 
IteratorUtils.toSet(set.iterator(), -1), "Expecting IllegalArgumentException");
+    }
+
     @Test
     public void testToListIterator() {
         final List<Integer> list = new ArrayList<>();

Reply via email to