从java中的二维数组流式传输
我正在尝试从 n 维 int
数组中获取 IntStream
.有没有很好的 API 方法来做到这一点?我知道两个流的连接方法.
I am trying to get an IntStream
out of an n dimensional int
arrays. Is there a nice API way to do it?
I know the concatenate method for two streams.
推荐答案
假设您想以行优先方式顺序处理数组数组,这应该可以:
Assuming you want to process array of array sequentially in row-major approach, this should work:
int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
IntStream stream = Arrays.stream(arr).flatMapToInt(x -> Arrays.stream(x));
首先它调用 Arrays.stream(T[])
方法,其中T
被推断为int[]
,得到一个Stream<int[]>
,然后是 Stream#flatMapToInt()
方法将每个 int[]
元素映射到一个 IntStream
使用 Arrays.stream(int[])
方法.
First it invokes the Arrays.stream(T[])
method, where T
is inferred as int[]
, to get a Stream<int[]>
, and then Stream#flatMapToInt()
method maps each int[]
element to an IntStream
using Arrays.stream(int[])
method.
相关文章