Java Arrays - parallelPrefix() Method
The java.util.Arrays.parallelPrefix() method is used to cumulate, in parallel, each element of the given array in place, using the supplied function. For example if the array initially holds [2.0, 1.0, 0.0, 3.0] and the operation performs addition, then upon return the array holds [2.0, 3.0, 3.0, 6.0].
Syntax
public static void parallelPrefix(double[] array, DoubleBinaryOperator op)
Parameters
array |
Specify the array, which is modified in-place by this method. |
op |
Specify a side-effect-free function to perform the cumulation. |
Return Value
void type.
Exception
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 of the 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 Arrays.parallelPrefix(Arr, (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 3.0 6.0 10.0 15.0
❮ Java.util - Arrays