public class ParallelCollectionExtensions
extends Object
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). 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.
| Type Params | Return Type | Name and description |
|---|---|---|
<T> |
public static boolean |
anyParallel(Collection<T> self, Predicate<T> predicate)Returns true if any element matches the predicate. |
<T, R> |
public static 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). |
<T, R> |
public static List<R> |
collectParallel(Collection<T> self, Function<T, R> transform)Transforms each element in parallel, returning a new list. |
<T> |
public static long |
countParallel(Collection<T> self, Predicate<T> predicate)Counts elements matching the predicate. |
<T> |
public static void |
eachParallel(Collection<T> self, Consumer<T> action)Iterates over the collection in parallel, applying the action to each element. |
<T> |
public static void |
eachWithIndexParallel(Collection<T> self, BiConsumer<T, Integer> action)Iterates over the collection in parallel with element indices. |
<T> |
public static boolean |
everyParallel(Collection<T> self, Predicate<T> predicate)Returns true if all elements match the predicate. |
<T> |
public static List<T> |
findAllParallel(Collection<T> self, Predicate<T> filter)Filters the collection in parallel, returning elements that match. |
<T> |
public static T |
findAnyParallel(Collection<T> self, Predicate<T> filter)Finds any element matching the predicate. |
<T> |
public static T |
findParallel(Collection<T> self, Predicate<T> filter)Finds the first element matching the predicate in encounter order. |
<T> |
public static List<T> |
grepParallel(Collection<T> self, Object filter)Filters elements using Groovy's isCase pattern matching
in parallel. |
<T, K> |
public static Map<K, List<T>> |
groupByParallel(Collection<T> self, Function<T, K> classifier)Groups elements by the classifier function in parallel. |
<T> |
public static T |
injectParallel(Collection<T> self, T seed, BinaryOperator<T> accumulator)Reduces the collection in parallel with a seed value. |
<T> |
public static T |
maxParallel(Collection<T> self, Comparator<T> comparator)Finds the maximum element using the given comparator. |
<T> |
public static T |
minParallel(Collection<T> self, Comparator<T> comparator)Finds the minimum element using the given comparator. |
<T> |
public static List<List<T>> |
splitParallel(Collection<T> self, Predicate<T> predicate)Partitions the collection into two lists: elements that match the predicate and elements that don't. |
<T> |
public static T |
sumParallel(Collection<T> self, BinaryOperator<T> accumulator)Reduces the collection in parallel using the given operator. |
Returns true if any element matches the predicate.
Transforms each element into a collection and flattens the results in parallel (parallel flatMap).
Transforms each element in parallel, returning a new list.
Counts elements matching the predicate.
Iterates over the collection in parallel, applying the action to each element.
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.
Returns true if all elements match the predicate.
Filters the collection in parallel, returning elements that match.
Finds any element matching the predicate. May be faster than findParallel as it does not preserve encounter order.
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. Use findAnyParallel if any match will do — it may be faster as it avoids ordering constraints.
Filters elements using Groovy's isCase pattern matching
in parallel. Supports the same filter types as Groovy's grep:
Class, regex Pattern, Range, Collection, Closure, etc.
filter - the pattern to match against (uses isCase)Groups elements by the classifier function in parallel.
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.
seed - the initial valueaccumulator - an associative reduction functionFinds the maximum element using the given comparator.
Finds the minimum element using the given comparator.
Partitions the collection into two lists: elements that match the predicate and elements that don't.
Reduces the collection in parallel using the given operator.