Java Arrays parallelPrefix(double[] array, DoubleBinaryOperator op) Method
Description
The Java Arrays parallelPrefix(double[] array, DoubleBinaryOperator op) method cumulates each element of the given array in a parallel fashion, using the supplied function. Being parallel, prefix computation is generally more efficient than sequential looping computation for large arrays.
Declaration
Following is the declaration for java.util.Arrays.parallelPrefix(double[] array, DoubleBinaryOperator op) method
public static void parallelPrefix(double[] array, DoubleBinaryOperator op)
Parameters
array − This is the array, which is modified in-place by this method.
op − This is a side-effect-free function to perform the cumulation.
Return Value
This method is not returning anything.
Exception
NullPointerException − if the specified array or function is null.
Java Arrays parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op) Method
Description
The Java Arrays parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op) method cumulates each element of the given array in a parallel fashion, using the supplied function, over a given range.
Declaration
Following is the declaration for java.util.Arrays.parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op) method
public static void parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)
Parameters
array − This is the array, which is modified in-place by this method.
fromIndex − This is the index of the first element (inclusive).
toIndex − This is the index of the last element (exclusive).
op − This is a side-effect-free function to perform the cumulation.
Return Value
This method is not returning anything.
Exception
IllegalArgumentException − if fromIndex > toIndex
ArrayIndexOutOfBoundsException − if fromIndex < 0 or toIndex > array.length
NullPointerException − if either specified array or function is null
Modifying Array of double Example
The following example shows the usage of Java Arrays parallelPrefix(double[], DoubleBinaryOperator) method. First, we've created an array of doubles, printed the original array. Using parallelPrefix() method, array is modified to have cumulative results. Modified array is printed thereafter.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
// initialize array
double arr[] = { 1.0, 2.0, 3.0, 5.0, 8.0 };
System.out.print("Original Array: [");
// print the array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("]");
Arrays.parallelPrefix(arr, (x, y) -> x + y);
System.out.print("Modified Array: [");
// print the array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("]");
}
}
Output
Let us compile and run the above program, this will produce the following result −
Original Array: [1.0 2.0 3.0 5.0 8.0 ] Modified Array: [1.0 3.0 6.0 11.0 19.0 ]
Modifying Array of doubles Using Range Example
The following example shows the usage of Java Arrays parallelPrefix(double[], int, int, DoubleBinaryOperator) method. First, we've created an array of doubles, printed the original array. Using parallelPrefix() method, array is modified to have cumulative results. Modified array is printed thereafter.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
// initialize array
double arr[] = { 1.0, 2.0, 3.0, 5.0, 8.0 };
System.out.print("Original Array: [");
// print the array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("]");
Arrays.parallelPrefix(arr, 0, arr.length, (x, y) -> x + y);
System.out.print("Modified Array: [");
// print the array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("]");
}
}
Output
Let us compile and run the above program, this will produce the following result −
Original Array: [1.0 2.0 3.0 5.0 8.0 ] Modified Array: [1.0 3.0 6.0 11.0 19.0 ]
Modifying Sub-Array of doubles Using Range Example
The following example shows the usage of Java Arrays parallelPrefix(double[], int, int, DoubleBinaryOperator) method. First, we've created an array of doubles, printed the original array. Using parallelPrefix() method, sub-array is modified to have cumulative results. Modified array is printed thereafter.
package com.tutorialspoint;
import java.util.Arrays;
public class ArrayDemo {
public static void main(String[] args) {
// initialize array
double arr[] = { 1.0, 2.0, 3.0, 5.0, 8.0 };
System.out.print("Original Array: [");
// print the array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("]");
Arrays.parallelPrefix(arr, 0, 4, (x, y) -> x + y);
System.out.print("Modified Array: [");
// print the array
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("]");
}
}
Output
Let us compile and run the above program, this will produce the following result −
Original Array: [1.0 2.0 3.0 5.0 8.0 ] Modified Array: [1.0 3.0 6.0 11.0 8.0 ]