Close

Java Collections - Arrays.parallelPrefix() Examples

Code Snippets Java Collections Java Java API 


Class:

java.util.Arrays

java.lang.Objectjava.lang.Objectjava.util.Arraysjava.util.ArraysLogicBig

Methods:

These methods apply cumulative operation, in parallel, on each two elements of the array.

public static <T> void parallelPrefix(T[] array,
                                      BinaryOperator<T> op)
public static <T> void parallelPrefix(T[] array,
                                      int fromIndex,
                                      int toIndex,
                                      BinaryOperator<T> op)
public static void parallelPrefix(long[] array,
                                  LongBinaryOperator op)
public static void parallelPrefix(long[] array,
                                  int fromIndex,
                                  int toIndex,
                                  LongBinaryOperator op)
public static void parallelPrefix(double[] array,
                                  DoubleBinaryOperator op)
public static void parallelPrefix(double[] array,
                                  int fromIndex,
                                  int toIndex,
                                  DoubleBinaryOperator op)
public static void parallelPrefix(int[] array,
                                  IntBinaryOperator op)
public static void parallelPrefix(int[] array,
                                  int fromIndex,
                                  int toIndex,
                                  IntBinaryOperator op)

Examples


package com.logicbig.example.arrays;

import java.util.Arrays;

public class ParallelPrefixExample {

public static void main(String... args) {
int[] arr1 = {3, 5, 6};

Arrays.parallelPrefix(arr1, (left, right) -> left * right);
System.out.println(Arrays.toString(arr1));
}
}

Output

[3, 15, 90]




Printing time to show that each operation is applied almost at same time (parallel)

package com.logicbig.example.arrays;

import java.time.LocalTime;
import java.util.Arrays;

public class ParallelPrefixExample2 {

public static void main(String... args) {
int[] arr1 = {3, 5, 6, 7, 8, 9, 10, 11};

Arrays.parallelPrefix(arr1, (left, right) -> {
System.out.println(LocalTime.now());
return left * right;
});
System.out.println(Arrays.toString(arr1));
}
}

Output

16:21:53.261
16:21:53.266
16:21:53.266
16:21:53.266
16:21:53.266
16:21:53.266
16:21:53.266
[3, 15, 90, 630, 5040, 45360, 453600, 4989600]




Index based method:

package com.logicbig.example.arrays;

import java.util.Arrays;

public class ParallelPrefixExample3 {

public static void main(String... args) {
int[] arr1 = {3, 5, 6, 7, 8, 9, 10, 11};

Arrays.parallelPrefix(arr1, 1, 4,
(left, right) -> left * right);
System.out.println(Arrays.toString(arr1));
}
}

Output

[3, 5, 30, 210, 8, 9, 10, 11]




Arrays#parallelPrefix(T[] array, ..) example:

package com.logicbig.example.arrays;

import java.util.Arrays;

public class ParallelPrefixExample4 {

public static void main(String... args) {
String[] arr = {"apple", "banana", "pie"};
System.out.println(Arrays.toString(arr));

Arrays.parallelPrefix(arr, (s, s2) -> s + "|" + s2);
System.out.println(Arrays.toString(arr));
}
}

Output

[apple, banana, pie]
[apple, apple|banana, apple|banana|pie]




See Also