从java中的二维数组流式传输 [英] Stream from two dimensional array in java

查看:17
本文介绍了从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.

这篇关于从java中的二维数组流式传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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