ParallelStream toArray更大的Java8 [英] ParallelStream toArray of higher size Java8

查看:163
本文介绍了ParallelStream toArray更大的Java8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从并行流创建一个更大尺寸的数组

I am trying to create an array with higher size from a parallel stream

myList.parallelStream().map((listElm) -> {
            return someObjectMorpher(listElm);}).toArray(size -> new SomeObject[size+4]);




  1. 是否保证数组的最后4个元素始终为空值?
    这意味着toArray将始终填充数组的第一个索引。

  2. 是否有可能必须使用toArray填充已创建的数组

SomeObjet [] arr = new SomeObject [myList.size()+ 4];
arr [0] = new SomeObject(x);
myList.parallelStream()。map((listElm) - > {
return someObjectMorpher(listElm);})。toArray(** /// use arr **);

推荐答案


是否可以保证数组的最后4个元素始终为空?

Is it guaranteed that the last 4 elements of the array will always be null?

基本上没有。数组的最后四个元素将保留其先前的值。如果值恰好是 null ,那么它们将保持 null 。例如:

Essentially, no. The last four elements of the array will retain their previous value. If the value happened to be null, then they will remain null. For example:

Integer[] array = Stream.of(7, 8).parallel().toArray(i -> new Integer[] {1, 2, 3, 4, 5, 6});

System.out.println(Arrays.toString(array));

输出:

[7, 8, 3, 4, 5, 6]




是否有可能让aStray填充已经创建的数组

Is there a possibility of having toArray populate an already created array

是的,见上文,以及另一个例子:

Yes, see above, as well as another example:

Integer[] array = new Integer[] {1, 2, 3, 4, 5, 6};

Integer[] newArray = Stream.of(7, 8).parallel().toArray(i -> array);

System.out.println(Arrays.toString(newArray));

输出:

[7, 8, 3, 4, 5, 6]

注意:如果数组的长度与流的大小不同,则使用顺序流尝试此操作将抛出 IllegalStateException

Note: Attempting this with sequential streams will throw an IllegalStateException if the length of the array differs from the size of the stream.

这篇关于ParallelStream toArray更大的Java8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆