Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OAK-11073 : added new api in CollectionUtils to create HashSet with expected… #1729

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading