'Merging streams that are stored in array

I have an array of streams. These streams include integers. I want to merge these streams using only one statement. Here is an example:

Stream<?>[] arr = new Stream<?>[3];
arr[0] = Stream.of(1, 2, 3, 4, 5, 6, 7);
arr[1] = Stream.of(1, 2, 3, 4, 5, 6, 7);
arr[2] = Stream.of(1, 2, 3, 4, 5, 6, 7);

Stream<?> mergedStream = Stream.concat(Stream.concat(arr[0], arr[1]), arr[2]);

The last line throws the following error: "finished with non-zero exit value 1" but I hope my intention is clear. However, I usually don't know the amount of streams that have to be merged. I was thinking about the following solution but it doesn't work:

Stream<?> mergedStream =Arrays.stream(arr).flatMap(Function.identity());


Solution 1:[1]

Maybe try Stream<Integer>[] arr = new Stream<Integer>[3];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Toomas Test