Java 8 Arrays parallelPrefix method Example

7 years ago Lalit Bhagtani 0

In this tutorial, we will learn about Java 8 Arrays parallelPrefix method.

Java 8 Arrays parallelPrefix

parallelPrefix() method :-

This method takes one array of Objects and one BinaryOperator as an argument. It apply passed BinaryOperator function on each element of the given array in parallel to update it. Let’s suppose, we have an array as [ 1, 3, 5, 7 ] and given BinaryOperator function is of multiplication then the result will be [ 1, 3, 15, 35 ].

Syntax :-  static void parallelPrefix ( T[] array, BinaryOperator<T> op )

Reference :-  parallelPrefix() method JavaDocs

Arrays class provides one more method, which takes two more arguments fromIndex ( int ) and toIndex ( int ). As name suggest, given function will be applied to the elements between these two indexes.

       Syntax :-  static void parallelPrefix ( T[] array, int fromIndex, int toIndex, BinaryOperator<T> op )

There are six specialized ( primitive specific ) form of Arrays parallelPrefix method, which can be used in case of int, long and double. They are as follows :-

  1. static void parallelPrefix ( double[] array, DoubleBinaryOperator op ) :- It apply given DoubleBinaryOperator function on each element of the given double array in parallel to update it.
  2. static void parallelPrefix ( double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op ) :- It apply given DoubleBinaryOperator function on elements from fromIndex ( inclusive ) to toIndex ( excluding ) of the given double array in parallel to update it.
  3. static void parallelPrefix ( long[] array, LongBinaryOperator op ) :- It apply given LongBinaryOperator function on each element of the given long array in parallel to update it.
  4. static void parallelPrefix ( long[] array, int fromIndex, int toIndex, LongBinaryOperator op ) :- It apply given LongBinaryOperator function on elements from fromIndex ( inclusive ) to toIndex ( excluding ) of the given long array in parallel to update it.
  5. static void parallelPrefix ( int[] array, IntBinaryOperator op ) :- It apply given IntBinaryOperator function on each element of the given int array in parallel to update it.
  6. static void parallelPrefix ( int[] array, int fromIndex, int toIndex, IntBinaryOperator op ) :- It apply given IntBinaryOperator function on each elements from fromIndex ( inclusive ) to toIndex ( excluding ) of the given int array in parallel to update it.

Example :-

import java.util.Arrays;
import java.util.function.BinaryOperator;
import java.util.function.IntBinaryOperator;

public class ParallelPrefixExample {
 
  public static void main(String[] args){
 
    /* Arrays parallelPrefix with array of primitive data (int) */
    IntBinaryOperator intBinaryOperator = ( x,y ) -> x * y; 
 
    int[] arr1 = {2,5,7,3,4,5,1};
    Arrays.parallelPrefix(arr1, intBinaryOperator);
    System.out.println("parallelprefix for int array :- ");
    Arrays.stream(arr1).forEach( x -> System.out.print(x+", "));
 
    System.out.println("");
 
    int[] arr2 = {2,5,7,3,4,5,1};
    Arrays.parallelPrefix(arr2, 2, 5, intBinaryOperator);
    System.out.println("parallelprefix for int array from index 2 to 5 :- ");
    Arrays.stream(arr2).forEach( x -> System.out.print(x+", "));
 
    System.out.println("");
 
    /* Arrays parallelPrefix with array of Point Object */
    BinaryOperator<Point> binaryOperator = ( x,y ) -> new Point(x.getX() + y.getX(), x.getY() + y.getY());
 
    Point[] arr3 = {new Point(10,10),new Point(20,20),new Point(30,30),new Point(40,40),new Point(50,50)};
    Arrays.parallelPrefix(arr3, binaryOperator);
    System.out.println("parallelprefix for Point array :- ");
    Arrays.stream(arr3).forEach( x -> System.out.print(x.getValue()+", "));
 
    System.out.println("");
 
    Point[] arr4 = {new Point(10,10),new Point(20,20),new Point(30,30),new Point(40,40),new Point(50,50)};
    Arrays.parallelPrefix(arr4, 1, 3, binaryOperator);
    System.out.println("parallelprefix for Point array from index 1 to 3 :- ");
    Arrays.stream(arr4).forEach( x -> System.out.print(x.getValue()+", "));
 
  } 
 
}

Point :-

public class Point {

  private int x;
  private int y;	
	
  public Point(int x, int y) {
    super();
    this.x = x;
    this.y = y;
  }
	
  public int getX() {
    return x;
  }
	
  public void setX(int x) {
    this.x = x;
  }
	
  public int getY() {
    return y;
  }
	
  public void setY(int y) {
    this.y = y;
  }
	
  public String getValue() {
    return x + "," + y;
  }
	
}

Result :- 

parallelprefix for int array :- 
2, 10, 70, 210, 840, 4200, 4200, 
parallelprefix for int array from index 2 to 5 :- 
2, 5, 7, 21, 84, 5, 1, 
parallelprefix for Point array :- 
10,10, 30,30, 60,60, 100,100, 150,150, 
parallelprefix for Point array from index 1 to 3 :- 
10,10, 20,20, 50,50, 40,40, 50,50,

Reference :-

  1. Arrays Class JavaDoc

That’s all for Java 8 Arrays parallelPrefix method. If you liked it, please share your thoughts in comments section and share it with others too.