Java Arrays parallelPrefix(int[] array, IntBinaryOperator op) Method



Description

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

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

Description

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

public static void parallelPrefix​(int[] array, int fromIndex, int toIndex, IntBinaryOperator 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 ints Example

The following example shows the usage of Java Arrays parallelPrefix(int[], IntBinaryOperator) method. First, we've created an array of ints, 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
      int arr[] = { 1, 2, 3, 5, 8 };

      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 ints Using Range Example

The following example shows the usage of Java Arrays parallelPrefix(int[], int, int, IntBinaryOperator) method. First, we've created an array of ints, 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
      int arr[] = { 1, 2, 3, 5, 8 };

      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 ints Using Range Example

The following example shows the usage of Java Arrays parallelPrefix(int[], int, int, IntBinaryOperator) method. First, we've created an array of ints, 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
      int arr[] = { 1, 2, 3, 5, 8 };

      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