Java Arrays parallelPrefix(long[] array, LongBinaryOperator op) Method



Description

The Java Arrays parallelPrefix(long[] array, LongBinaryOperator 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(long[] array, LongBinaryOperator op) method

 public static void parallelPrefix(long[] array, LongBinaryOperator 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(long[] array, int fromIndex, int toIndex, LongBinaryOperator op) Method

Description

The Java Arrays parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator 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(long[] array, int fromIndex, int toIndex, LongBinaryOperator op) method

 public static void parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator 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 longs Example

The following example shows the usage of Java Arrays parallelPrefix(long[], LongBinaryOperator) method. First, we've created an array of longs, 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 long arr[] = { 1L, 2L, 3L, 5L, 8L }; 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 2 3 5 8 ] Modified Array: [1 3 6 11 19 ] 

Modifying Array of longs Using Range Example

The following example shows the usage of Java Arrays parallelPrefix(long[], int, int, LongBinaryOperator) method. First, we've created an array of longs, 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 long arr[] = { 1L, 2L, 3L, 5L, 8L }; 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 2 3 5 8 ] Modified Array: [1 3 6 11 19 ] 

Modifying Sub-Array of longs Using Range Example

The following example shows the usage of Java Arrays parallelPrefix(long[], int, int, LongBinaryOperator) method. First, we've created an array of longs, 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 long arr[] = { 1L, 2L, 3L, 5L, 8L }; 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 2 3 5 8 ] Modified Array: [1 3 6 11 8 ] 
java_util_arrays.htm
Advertisements