Skip to content

Commit

Permalink
Merge pull request #1744 from apache/OAK-11153
Browse files Browse the repository at this point in the history
OAK-11153 : added utils to create LinkedHashSet/hashSet with expected…
  • Loading branch information
rishabhdaim authored Oct 2, 2024
2 parents 946c8d8 + 65cbc45 commit 35950be
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,6 +87,20 @@ public static <T> Set<T> toSet(@NotNull final Iterable<T> 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 <T> the type of the elements
*/
@NotNull
public static <T> Set<T> toLinkedSet(@NotNull final Iterable<T> iterable) {
Objects.requireNonNull(iterable);
final Set<T> 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
Expand Down Expand Up @@ -118,6 +133,38 @@ public static <T> Set<T> toSet(@NotNull final T... elements) {
return result;
}

/**
* Creates a new, empty HashSet with expected capacity.
* <p>
* 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 <K> Set<K> 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.
* <p>
* 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 <K> Set<K> 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.
* <p>
Expand All @@ -126,10 +173,12 @@ public static <T> Set<T> 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 <K, V> Map<K, V> 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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -62,6 +63,15 @@ public void iterableToSet() {
Assert.assertEquals(s, CollectionUtils.toSet(iterable));
}

@Test
public void iterableToLinkedSet() {
// create an iterable
final Set<String> s = new LinkedHashSet<>(data);
final Iterable<String> iterable = new SimpleIterable<>(s);

Assert.assertEquals(s, CollectionUtils.toLinkedSet(iterable));
}

@Test
public void iteratorToSet() {
// create an iterable
Expand Down

0 comments on commit 35950be

Please sign in to comment.