Signup/Sign In

Java Arrays parallelPrefix() Method

In this article, we will learn about parallelPrefix() method of Arrays class in Java. This method performs operations on array elements with a current element and a previous element. This method is more efficient than the loop when we need to perform operations on the whole array.

Syntax

The following method will receive an array and IntBinaryOperator. This operator performs an operation of two operands. This operand can perform simple operations, as simple as an arithmetic operation.

static void	parallelPrefix?(int[] array, IntBinaryOperator op)

List of the Overloading Methods of parallelPrefix() Method

This table contains all the overloaded variants of parallelPrefix() method.

Method Description

static void parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)

This method performs parallelPrefix(double[], DoubleBinaryOperator) for the given subrange of the array.

static void parallelPrefix(double[] array, DoubleBinaryOperator op)

This method cumulates, in parallel, each element of the given array in place, using the supplied function.

static void parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)

This method performs parallelPrefix(int[], IntBinaryOperator) for the given subrange of the array.

static void parallelPrefix(int[] array, IntBinaryOperator op)

This method cumulates, in parallel, each element of the given array in place, using the supplied function.

static void parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op)

This method performs parallelPrefix(long[], LongBinaryOperator) for the given subrange of the array.

static void parallelPrefix(long[] array, LongBinaryOperator op)

This method cumulates, in parallel, each element of the given array in place, using the supplied function.

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

This method performs parallelPrefix(Object[], BinaryOperator) for the given subrange of the array.

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

This method cumulates, in parallel, each element of the given array in place, using the supplied function.

Example of parallelPrefix() Method

In the following example, we can see the given array arr have numbers 1 to 7 then we are creating IntBinaryOperator of addition and then we applied parallelPrefix() on the array and it will change to sequential sum.

import java.util.Arrays;
import java.util.function.IntBinaryOperator;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
        int arr[] = { 1, 2, 3, 4, 5, 6, 7};
        IntBinaryOperator op = (x, y) -> x + y;
        Arrays.parallelPrefix(arr,op); 
        for(int num:arr)
        {
        	System.out.print(num+" ");
        }
	} 
}


1 3 6 10 15 21 28

Example of parallelPrefix() method by specifying a range

As most of the methods of Array class support to specify the range of array we have another overloading method of parallelPrefix() where we pass startIndex and endIndex. In the following program, changes will be made only on the array elements from 2nd index to 10th index.

import java.util.Arrays;
import java.util.function.IntBinaryOperator;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        IntBinaryOperator op = (x, y) -> x + y;
        Arrays.parallelPrefix(arr,2,10,op); 
        for(int num:arr)
        {
        	System.out.print(num+" ");
        }
	} 
}


1 2 3 7 12 18 25 33 42 52 11 12 13 14 15

Example of Its Overloaded Methods

There are other overloading methods to support different types of arrays and ranges.

This method can receive an array of primitive and no-primitive data types and perform the operation defined by BinaryOperator.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{ 
		//Example static void parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op)
		double doubleArray[] = {5.1, 6.2, 7.2, 8.1, 9.4, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2}; 
		Arrays.parallelPrefix(doubleArray, 0, 5, (n1, n2) -> n1 + n2);
		System.out.println(Arrays.toString(doubleArray));

		//Example static void parallelPrefix(double[] array, DoubleBinaryOperator op)
		Arrays.parallelPrefix(doubleArray, (n1, n2) -> n1 + n2);
		System.out.println(Arrays.toString(doubleArray));

		//Example static void parallelPrefix(int[] array,int fromIndex, int toIndex, IntBinaryOperator op)
		int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
		Arrays.parallelPrefix(intArray, 2, 6, (n1, n2) -> n1 + n2);
		System.out.println(Arrays.toString(intArray));

		//Example static void parallelPrefix(int[] array, IntBinaryOperator op)
		Arrays.parallelPrefix(intArray, (n1, n2) -> n1 + n2);
		System.out.println(Arrays.toString(intArray));

		//Example static void parallelPrefix(long[] array,int fromIndex, int toIndex, LongBinaryOperator op)
		long longArray[] = {11, 12, 31, 41, 15, 16, 17, 18, 19, 18};
		Arrays.parallelPrefix(longArray, 3, 8, (n1, n2) -> n1 + n2);
		System.out.println(Arrays.toString(longArray));

		//Example static void parallelPrefix(long[] array, LongBinaryOperator op)
		Arrays.parallelPrefix(longArray, (n1, n2) -> n1 + n2);
		System.out.println(Arrays.toString(longArray));


	}
}


[5.1, 11.3, 18.5, 26.6, 36.0, 10.2, 11.6, 12.96, 13.2, 14.25, 15.6, 16.4, 17.2]
[5.1, 16.4, 34.9, 61.5, 97.5, 107.7, 119.3, 132.26, 145.45999999999998, 159.70999999999998, 175.30999999999997, 191.70999999999998, 208.90999999999997]
[1, 2, 3, 7, 12, 18, 7, 8, 9, 10]
[1, 3, 6, 13, 25, 43, 50, 58, 67, 77]
[11, 12, 31, 41, 56, 72, 89, 107, 19, 18]
[11, 23, 54, 95, 151, 223, 312, 419, 438, 456]

Conclusion

In this tutorial, we learned how to use parallelPrefix() method. We can also use various overloading methods with different array types. To perform the sequential operation on the array we need BinaryOperator and this can be defined in multiple ways for specific tasks.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.