diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java index f08ba6cfa17..f49d0b7908b 100644 --- a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java +++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Objects; @@ -86,6 +87,20 @@ public static Set toSet(@NotNull final Iterable iterable) { return result; } + /** + * Convert an iterable to a {@link LinkedHashSet}. The returning set is mutable and supports all optional operations. + * @param iterable the iterable to convert + * @return the linkedHashSet + * @param the type of the elements + */ + @NotNull + public static Set toLinkedSet(@NotNull final Iterable iterable) { + Objects.requireNonNull(iterable); + final Set result = new LinkedHashSet<>(); + iterable.forEach(result::add); + return result; + } + /** * Convert an iterator to a set. The returning set is mutable and supports all optional operations. * @param iterator the iterator to convert @@ -118,6 +133,38 @@ public static Set toSet(@NotNull final T... elements) { return result; } + /** + * Creates a new, empty HashSet with expected capacity. + *

+ * The returned set is large enough to add expected no. of elements without resizing. + * + * @param capacity the expected number of elements + * @throws IllegalArgumentException if capacity is negative + * @see CollectionUtils#newHashMap(int) + * @see CollectionUtils#newLinkedHashSet(int) + */ + @NotNull + public static Set newHashSet(final int capacity) { + // make sure the set does not need to be resized given the initial content + return new HashSet<>(ensureCapacity(capacity)); + } + + /** + * Creates a new, empty LinkedHashSet with expected capacity. + *

+ * The returned set is large enough to add expected no. of elements without resizing. + * + * @param capacity the expected number of elements + * @throws IllegalArgumentException if capacity is negative + * @see CollectionUtils#newHashMap(int) + * @see CollectionUtils#newHashSet(int) + */ + @NotNull + public static Set newLinkedHashSet(final int capacity) { + // make sure the set does not need to be resized given the initial content + return new LinkedHashSet<>(ensureCapacity(capacity)); + } + /** * Creates a new, empty HashMap with expected capacity. *

@@ -126,10 +173,12 @@ public static Set toSet(@NotNull final T... elements) { * * @param capacity the expected number of elements * @throws IllegalArgumentException if capacity is negative + * @see CollectionUtils#newHashSet(int) + * @see CollectionUtils#newLinkedHashSet(int) */ @NotNull public static Map newHashMap(final int capacity) { - // make sure the set does not need to be resized given the initial content + // make sure the Map does not need to be resized given the initial content return new HashMap<>(ensureCapacity(capacity)); } diff --git a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java index 6b943c02bc1..dd0fb1d1a16 100644 --- a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java +++ b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -62,6 +63,15 @@ public void iterableToSet() { Assert.assertEquals(s, CollectionUtils.toSet(iterable)); } + @Test + public void iterableToLinkedSet() { + // create an iterable + final Set s = new LinkedHashSet<>(data); + final Iterable iterable = new SimpleIterable<>(s); + + Assert.assertEquals(s, CollectionUtils.toLinkedSet(iterable)); + } + @Test public void iteratorToSet() { // create an iterable