Java Utility Library

Java Arrays - parallelPrefix() Method



The java.util.Arrays.parallelPrefix() method is used to perform parallelPrefix(double[], DoubleBinaryOperator) for the given subrange of the array.

Syntax

public static void parallelPrefix(double[] array,
                                  int fromIndex,
                                  int toIndex,
                                  DoubleBinaryOperator op)

Parameters

array Specify the array.
fromIndex Specify the index of the first element, inclusive.
toIndex Specify the index of the last element, exclusive.
op Specify a side-effect-free, associative function to perform the cumulation.

Return Value

void type.

Exception

  • Throws IllegalArgumentException, if fromIndex > toIndex.
  • Throws ArrayIndexOutOfBoundsException, if fromIndex < 0 or toIndex > array.length.
  • Throws NullPointerException, if the specified array or function is null.

Example:

In the example below, the java.util.Arrays.parallelPrefix() method is used to add, in parallel, each element in the given range of given array in place.

import java.util.*;

public class MyClass {
  
  //Adds two double numbers 
  static double MyFunc(double x, double y) { 
    return x + y; 
  } 

  public static void main(String[] args) {
    //creating a double array
    double Arr[] = {1d, 2d, 3d, 4d, 5d};

    //printing Arr
    System.out.print("Arr contains:"); 
    for(double i: Arr)
      System.out.print(" " + i);
      
    //MyFunc is used with parallelPrefix method to add
    //each elements of the array in index range [2,5)
    Arrays.parallelPrefix(Arr, 2, 5, (a,b) -> MyFunc(a,b));

    //printing Arr
    System.out.print("\nArr contains:"); 
    for(double i: Arr)
      System.out.print(" " + i);
  }
}

The output of the above code will be:

Arr contains: 1.0 2.0 3.0 4.0 5.0
Arr contains: 1.0 2.0 3.0 7.0 12.0

❮ Java.util - Arrays