Package: org.codehaus.groovy.runtime

## [Java] Class ParallelCollectionExtensions

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

- - ### Methods Summary

    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.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### <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. Index assignment is based on the collection's iteration order, but execution order is not guaranteed.

    - #### <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. May be faster than findParallel as it does not preserve encounter order.

    - #### <T> public static 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 if any match will do — it may be faster as it avoids ordering constraints.

    - #### <T> public static 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`)

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

      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

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

      Returns:
        a list of two lists: [matching, non-matching]

    - #### <T> public static T sumParallel(Collection<T> self, BinaryOperator<T> accumulator)

      Reduces the collection in parallel using the given operator.