Package org.codehaus.groovy.runtime

# Class ParallelCollectionExtensions

java.lang.Object
org.codehaus.groovy.runtime.ParallelCollectionExtensions

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(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.

Blocking work: closures that block (I/O, locks, barriers, timed waits) hold ForkJoin worker threads. Prefer a dedicated `ParallelScope.withPool(n)` with `n` large enough for the expected concurrent blockers, or an I/O-oriented pool (`Pool.io()`, `Pool.virtual()`) for blocking workloads. Relying on the shared common pool for blocking bodies can stall under contention.

Inspired by GPars' `GParsPoolUtil` category methods.

Since:
  6.0.0

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ParallelCollectionExtensions()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static <T> boolean`
  `anyParallel(Collection<T> self, Predicate<T> predicate)`
  Returns `true` if 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> long`
  `countParallel(Collection<T> self, Predicate<T> predicate)`
  Counts elements matching the predicate.
  `static <T> void`
  `eachParallel(Collection<T> self, Consumer<T> action)`
  Iterates over the collection in parallel, applying the action to each element.
  `static <T> void`
  `eachWithIndexParallel(Collection<T> self, BiConsumer<T,Integer> action)`
  Iterates over the collection in parallel with element indices.
  `static <T> boolean`
  `everyParallel(Collection<T> self, Predicate<T> predicate)`
  Returns `true` if 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> T`
  `findAnyParallel(Collection<T> self, Predicate<T> filter)`
  Finds any element matching the predicate.
  `static <T> T`
  `findParallel(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's `isCase` pattern matching in parallel.
  `static <T, K> Map<K,List<T>>`
  `groupByParallel(Collection<T> self, Function<T,K> classifier)`
  Groups elements by the classifier function in parallel.
  `static <T> T`
  `injectParallel(Collection<T> self, T seed, BinaryOperator<T> accumulator)`
  Reduces the collection in parallel with a seed value.
  `static <T> T`
  `maxParallel(Collection<T> self, Comparator<T> comparator)`
  Finds the maximum element using the given comparator.
  `static <T> T`
  `minParallel(Collection<T> self, Comparator<T> comparator)`
  Finds the minimum element using the given comparator.
  `static <T> 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.
  `static <T> T`
  `sumParallel(Collection<T> self, BinaryOperator<T> accumulator)`
  Reduces the collection in parallel using the given operator.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### ParallelCollectionExtensions

    public ParallelCollectionExtensions()

- ## Method Details

  - ### eachParallel

    public static <T> void eachParallel(Collection<T> self, Consumer<T> action)
    Iterates over the collection in parallel, applying the action to each element.

  - ### collectParallel

    public static <T, R> List<R> collectParallel(Collection<T> self, Function<T,R> transform)
    Transforms each element in parallel, returning a new list.

  - ### findAllParallel

    public static <T> List<T> findAllParallel(Collection<T> self, Predicate<T> filter)
    Filters the collection in parallel, returning elements that match.

  - ### findParallel

    public static <T> T findParallel(Collection<T> self, Predicate<T> filter)
    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(java.util.Collection<T>, java.util.function.Predicate<T>)` if any match will do — it may be faster as it avoids ordering constraints.

  - ### findAnyParallel

    public static <T> T findAnyParallel(Collection<T> self, Predicate<T> filter)
    Finds any element matching the predicate. May be faster than `findParallel(java.util.Collection<T>, java.util.function.Predicate<T>)` as it does not preserve encounter order.

  - ### anyParallel

    public static <T> boolean anyParallel(Collection<T> self, Predicate<T> predicate)
    Returns `true` if any element matches the predicate.

  - ### everyParallel

    public static <T> boolean everyParallel(Collection<T> self, Predicate<T> predicate)
    Returns `true` if all elements match the predicate.

  - ### countParallel

    public static <T> long countParallel(Collection<T> self, Predicate<T> predicate)
    Counts elements matching the predicate.

  - ### minParallel

    public static <T> T minParallel(Collection<T> self, Comparator<T> comparator)
    Finds the minimum element using the given comparator.

  - ### maxParallel

    public static <T> T maxParallel(Collection<T> self, Comparator<T> comparator)
    Finds the maximum element using the given comparator.

  - ### sumParallel

    public static <T> T sumParallel(Collection<T> self, BinaryOperator<T> accumulator)
    Reduces the collection in parallel using the given operator.

  - ### groupByParallel

    public static <T, K> Map<K,List<T>> groupByParallel(Collection<T> self, Function<T,K> classifier)
    Groups elements by the classifier function in parallel.

  - ### eachWithIndexParallel

    public static <T> void eachWithIndexParallel(Collection<T> self, BiConsumer<T,Integer> action)
    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

    public static <T> 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.
    Returns:
      a list of two lists: [matching, non-matching]

  - ### injectParallel

    public static <T> T injectParallel(Collection<T> self, T seed, BinaryOperator<T> accumulator)
    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 value
      `accumulator` - an associative reduction function

  - ### grepParallel

    public static <T> List<T> grepParallel(Collection<T> self, Object filter)
    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.
    Parameters:
      `filter` - the pattern to match against (uses `isCase`)