Skip to content

Commit

Permalink
OAK-11226 : added new api in CollectionUtils to create concurrent has…
Browse files Browse the repository at this point in the history
…h set
  • Loading branch information
Rishabh Kumar committed Oct 25, 2024
1 parent 2d33df6 commit 618b0ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
Expand Down Expand Up @@ -221,6 +222,20 @@ public static <K> Set<K> newHashSet(final int capacity) {
return new HashSet<>(ensureCapacity(capacity));
}

/**
* Creates a new, empty {@link Set} which is backed by {@link ConcurrentHashMap} to allow concurrent access.
* Returning Set doesn't allow null keys and values.
*
* @return a new, empty {@link Set} which is backed by {@link ConcurrentHashMap}.
*
* @see CollectionUtils#newHashMap(int)
* @see CollectionUtils#newLinkedHashSet(int)
*/
@NotNull
public static <K> Set<K> newConcurrentHashSet() {
return ConcurrentHashMap.newKeySet();
}

/**
* Creates a new, empty LinkedHashSet with expected capacity.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public void iteratorToSet() {
Assert.assertEquals(s, CollectionUtils.toSet(iterable.iterator()));
}

@Test
public void concurrentHashSet() {
// create a set of non-null values
final Set<String> s = data.stream().filter(Objects::nonNull).collect(Collectors.toSet());

Set<String> concurrentHashSet = CollectionUtils.newConcurrentHashSet();
concurrentHashSet.addAll(s);

Assert.assertEquals(s, concurrentHashSet);
}

@Test
public void toArrayDequeWithNonEmptyIterable() {
List<String> list = Arrays.asList("one", "two", "three");
Expand Down

0 comments on commit 618b0ba

Please sign in to comment.