Package org.codehaus.groovy.runtime
Class ParallelCollectionExtensions
java.lang.Object
org.codehaus.groovy.runtime.ParallelCollectionExtensions
DGM-like extension methods that add parallel collection operations
to
Collection.
These methods use Pool.current() to obtain the current pool
(typically set by ParallelScope.withPool(int, java.util.function.Function<groovy.concurrent.AsyncScope, T>)).
If no pool is current, they fall back to ForkJoinPool.commonPool().
All methods delegate to Java parallel streams with pool isolation:
the stream operations run on the current pool's ForkJoinPool,
not the common pool.
Inspired by GPars' GParsPoolUtil category methods.
- Since:
- 6.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> booleananyParallel(Collection<T> self, Predicate<T> predicate) Returnstrueif any element matches the predicate.static <T,R> List<R> collectManyParallel(Collection<T> self, Function<T, ? extends Collection<R>> transform) Transforms each element into a collection and flattens the results in parallel (parallel flatMap).static <T,R> List<R> collectParallel(Collection<T> self, Function<T, R> transform) Transforms each element in parallel, returning a new list.static <T> longcountParallel(Collection<T> self, Predicate<T> predicate) Counts elements matching the predicate.static <T> voideachParallel(Collection<T> self, Consumer<T> action) Iterates over the collection in parallel, applying the action to each element.static <T> voideachWithIndexParallel(Collection<T> self, BiConsumer<T, Integer> action) Iterates over the collection in parallel with element indices.static <T> booleaneveryParallel(Collection<T> self, Predicate<T> predicate) Returnstrueif all elements match the predicate.static <T> List<T>findAllParallel(Collection<T> self, Predicate<T> filter) Filters the collection in parallel, returning elements that match.static <T> TfindAnyParallel(Collection<T> self, Predicate<T> filter) Finds any element matching the predicate.static <T> TfindParallel(Collection<T> self, Predicate<T> filter) Finds the first element matching the predicate in encounter order.static <T> List<T>grepParallel(Collection<T> self, Object filter) Filters elements using Groovy'sisCasepattern matching in parallel.groupByParallel(Collection<T> self, Function<T, K> classifier) Groups elements by the classifier function in parallel.static <T> TinjectParallel(Collection<T> self, T seed, BinaryOperator<T> accumulator) Reduces the collection in parallel with a seed value.static <T> TmaxParallel(Collection<T> self, Comparator<T> comparator) Finds the maximum element using the given comparator.static <T> TminParallel(Collection<T> self, Comparator<T> comparator) Finds the minimum element using the given comparator.splitParallel(Collection<T> self, Predicate<T> predicate) Partitions the collection into two lists: elements that match the predicate and elements that don't.static <T> TsumParallel(Collection<T> self, BinaryOperator<T> accumulator) Reduces the collection in parallel using the given operator.
-
Constructor Details
-
ParallelCollectionExtensions
public ParallelCollectionExtensions()
-
-
Method Details
-
eachParallel
Iterates over the collection in parallel, applying the action to each element. -
collectParallel
Transforms each element in parallel, returning a new list. -
findAllParallel
Filters the collection in parallel, returning elements that match. -
findParallel
Finds the first element matching the predicate in encounter order. Although evaluation happens in parallel, the result is the matching element with the lowest index. UsefindAnyParallel(java.util.Collection<T>, java.util.function.Predicate<T>)if any match will do — it may be faster as it avoids ordering constraints. -
findAnyParallel
Finds any element matching the predicate. May be faster thanfindParallel(java.util.Collection<T>, java.util.function.Predicate<T>)as it does not preserve encounter order. -
anyParallel
Returnstrueif any element matches the predicate. -
everyParallel
Returnstrueif all elements match the predicate. -
countParallel
Counts elements matching the predicate. -
minParallel
Finds the minimum element using the given comparator. -
maxParallel
Finds the maximum element using the given comparator. -
sumParallel
Reduces the collection in parallel using the given operator. -
groupByParallel
Groups elements by the classifier function in parallel. -
eachWithIndexParallel
Iterates over the collection in parallel with element indices. Index assignment is based on the collection's iteration order, but execution order is not guaranteed. -
collectManyParallel
public static <T,R> List<R> collectManyParallel(Collection<T> self, Function<T, ? extends Collection<R>> transform) Transforms each element into a collection and flattens the results in parallel (parallel flatMap). -
splitParallel
Partitions the collection into two lists: elements that match the predicate and elements that don't.- Returns:
- a list of two lists: [matching, non-matching]
-
injectParallel
Reduces the collection in parallel with a seed value.Note: the accumulator must be associative for correct parallel results. Non-associative accumulators will produce undefined results.
- Parameters:
seed- the initial valueaccumulator- an associative reduction function
-
grepParallel
Filters elements using Groovy'sisCasepattern matching in parallel. Supports the same filter types as Groovy'sgrep: Class, regex Pattern, Range, Collection, Closure, etc.- Parameters:
filter- the pattern to match against (usesisCase)
-