Java 8 Arrays.parallelPrefix()方法示例

x33g5p2x  于2022-09-28 转载在 Java  
字(3.4k)|赞(0)|评价(0)|浏览(496)

在本文中中,我们将提供 java 8 Arrays 并行前缀示例。 Arrays.parallelPrefix() 根据给定的运算符更新数组。假设我们有一个数组 [2,1,3,5] 并且我们正在执行加法运算,那么结果将是 [2,3,6,11]。我们传递的操作符有BinaryOperatorIntBinaryOperatorDoubleBinaryOperator等。找到方法详情。
1.:它使用 BinaryOperator 来更新对象数组。

void parallelPrefix(T[] array, BinaryOperator&ltT> op)

2.:在这里我们可以从和到索引传递。

void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator&ltT> op)

3.:它更新原始数据类型的数组。

void  parallelPrefix(int[] array, IntBinaryOperator op)

4.:在这里我们可以从和到索引传递。

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

Arrays.parallelPrefix() 也用于其他原始数据类型,例如 long、float、double。

Arrays.parallelPrefix()对象数组

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&ltFloor> 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&ltFloor> 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);		
	}
}

地板.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;
	}
}

输出

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()原始数据类型数组

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);	
	}
}

输出

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

相关文章