Home  >  Java 8

Java 8 Arrays Parallel Prefix Example

By Arvind Rai, August 23, 2016
On this page we will provide java 8 Arrays parallel prefix example. Arrays.parallelPrefix() updates the array on the basis of given operator. Suppose we have an array as [2,1,3,5] and we are performing addition operation, then the result will be [2,3,6,11]. The operators we pass are BinaryOperator, IntBinaryOperator, DoubleBinaryOperator etc. Find the method details.
1.: It uses BinaryOperator to update the array of objects.
void parallelPrefix(T[] array, BinaryOperator<T> op)
 

2.: Here we can pass from and to index.
void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op)
 

3.: It update the array of primitive data types.
void  parallelPrefix(int[] array, IntBinaryOperator op)
 

4.: Here we can pass from and to index.
void parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)
 
Arrays.parallelPrefix() are also used with other primitive data types such as long, float, double.

Arrays.parallelPrefix() with Array of Objects


ParallelPrefixDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.function.BinaryOperator;
import java.util.function.Consumer;
public class ParallelPrefixDemo {
	public static void main(String[] args) {
		BinaryOperator<Floor> opt = (f1, f2) -> new Floor(f1.getLength() + f2.getLength(),
				f1.getWidth() + f2.getWidth());
		Floor[] floors = Floor.getFloorSizes();
		System.out.println("parallel prefix for complete array");
		Arrays.parallelPrefix(floors, opt);
		Consumer<Floor> print = f-> System.out.println(f.getLength()+", "+f.getWidth());
		Arrays.stream(floors).forEach(print);
		
		System.out.println("parallel prefix for array from index 1 to 4");
		floors = Floor.getFloorSizes();
		Arrays.parallelPrefix(floors, 1, 4, opt);
		Arrays.stream(floors).forEach(print);		
	}
} 
Floor.java
package com.concretepage;
public class Floor {
	private int length;
	private int width;
	public Floor(int length, int width) {
		this.length = length;
		this.width = width;
	}
	public int getLength() {
		return length;
	}
	public int getWidth() {
		return width;
	}
	public static Floor[] getFloorSizes() {
		Floor[] floors = new Floor[6];
		floors[0] = new Floor(2, 3);
		floors[1] = new Floor(4, 5);
		floors[2] = new Floor(3, 6);
		floors[3] = new Floor(2, 1);
		floors[4] = new Floor(1, 7);
		floors[5] = new Floor(4, 3);
		return floors;
	}
} 
Output
parallel prefix for complete array
2, 3
6, 8
9, 14
11, 15
12, 22
16, 25
parallel prefix for array from index 1 to 4
2, 3
4, 5
7, 11
9, 12
1, 7
4, 3 

Arrays.parallelPrefix() with Array of Primitive Data Types


ParallelPrefixDemoWithPrimitiveData.java
package com.concretepage;
import java.util.Arrays;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleConsumer;
import java.util.function.IntBinaryOperator;
import java.util.function.IntConsumer;
public class ParallelPrefixDemoWithPrimitiveData {
	public static void main(String[] args) {
		int[] intNum1 = {3,4,2,5,1,6,3};
		IntBinaryOperator intOpt = (i1, i2) -> i1 * i2;
		System.out.println("parallel prefix for complete array");
		Arrays.parallelPrefix(intNum1, intOpt);
		IntConsumer intCon = i -> System.out.print(i+" ");
		Arrays.stream(intNum1).forEach(intCon);
		
		System.out.println("\nparallel prefix for array from index 1 to 4");
		int[] intNum2 = {3,4,2,5,1,6,3};
		Arrays.parallelPrefix(intNum2, 1, 4, intOpt);
		Arrays.stream(intNum2).forEach(intCon);
		
		double[] dbNum1 = {3.2,4.1,2.2,5.4,1.2,6.4,3.2};
		DoubleBinaryOperator dbOpt = (d1, d2) -> d1 + d2;
		System.out.println("parallel prefix for complete array");
		Arrays.parallelPrefix(dbNum1, dbOpt);
		DoubleConsumer dbCon = d -> System.out.print(d+" ");
		Arrays.stream(dbNum1).forEach(dbCon);
		
		System.out.println("\nparallel prefix for array from index 1 to 4");
		double[] dbNum2 = {3.2,4.1,2.2,5.4,1.2,6.4,3.2};
		Arrays.parallelPrefix(dbNum2, 1, 4, dbOpt);
		Arrays.stream(dbNum2).forEach(dbCon);	
	}
} 
Output
parallel prefix for complete array
3 12 24 120 120 720 2160 
parallel prefix for array from index 1 to 4
3 4 8 40 1 6 3 parallel prefix for complete array
3.2 7.3 9.5 14.9 16.1 22.5 25.7 
parallel prefix for array from index 1 to 4
3.2 4.1 6.3 11.7 1.2 6.4 3.2  

Reference

Java Doc: Class Arrays
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us