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, 1, 0, 3] and the operation performs addition, then upon return the array holds [2, 3, 3, 6].
Syntax
public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op)
Here, T is the type of elements in the array.
Parameters
array |
Specify the array, which is modified in-place by this method. |
op |
Specify a side-effect-free, associative 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 Long numbers static Long MyFunc(Long x, Long y) { return x + y; } public static void main(String[] args) { //creating a Long array Long[] Arr = {1L, 2L, 3L, 4L, 5L}; //printing Arr System.out.print("Arr contains:"); for(Long 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(Long i: Arr) System.out.print(" " + i); } }
The output of the above code will be:
Arr contains: 1 2 3 4 5 Arr contains: 1 3 6 10 15
❮ Java.util - Arrays