ParallelStream toArray of higher size Java8

2019-07-24 21:07发布

问题:

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. Is it guaranteed that the last 4 elements of the array will always be null? Which means toArray will always populate at first indices of the Array.
  2. Is there a possibility of having toArray populate an already created array

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

回答1:

Is it guaranteed that the last 4 elements of the array will always be 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));

Output:

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

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

Output:

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

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



回答2:

Update It seems based on this question: Why can I collect a parallel stream to an arbitrarily large array but not a sequential stream?

That

toArray(size -> new Integer[size + 4])

Is breaking the contract of the generator https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#toArray-java.util.function.IntFunction-

generator - a function which produces a new array of the desired type and the provided length

So even though the assertions below pass, the code below should not be used.

Original response

Is it guaranteed that the last 4 elements of the array will always be null? Which means toArray will always populate at first indices of the Array.

This does work if you provide a generator function to allocate a new array of your desired size.

    Integer[] result = Arrays.asList(3,4,5).parallelStream().map(i -> i + 1).toArray(size -> new Integer[size + 4]);
    assert(result.length == 7);
    assert(result[3] == null);
    assert(result[4] == null);
    assert(result[5] == null);
    assert(result[6] == null);

All of these assertions pass.