Java 8 Stream 和对数组的操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24390463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:55:27  来源:igfitidea点击:

Java 8 Stream and operation on arrays

javaarraysjava-8java-stream

提问by BlackLabrador

I have just discovered the new Java 8 stream capabilities. Coming from Python, I was wondering if there was now a neat way to do operations on arrays like summing, multiplying two arrays in a "one line pythonic" way ?

我刚刚发现了新的 Java 8 流功能。来自 Python,我想知道现在是否有一种巧妙的方法来对数组进行运算,例如求和,以“一行 Pythonic”方式将两个数组相乘?

Thanks

谢谢

采纳答案by dkatzel

There are new methods added to java.util.Arraysto convert an array into a Java 8 stream which can then be used for summing etc.

添加了新方法java.util.Arrays以将数组转换为 Java 8 流,然后可将其用于求和等。

int sum =  Arrays.stream(myIntArray)
                 .sum();

Multiplying two arrays is a little more difficult because I can't think of a way to get the value AND the index at the same time as a Stream operation. This means you probably have to stream over the indexes of the array.

将两个数组相乘有点困难,因为我想不出在 Stream 操作的同时获取值和索引的方法。这意味着您可能必须对数组的索引进行流式处理。

//in this example a[] and b[] are same length
int[] a = ...
int[] b = ...

int[] result = new int[a.length];

IntStream.range(0, a.length)
         .forEach(i -> result[i] = a[i] * b[i]);

EDIT

编辑

Commenter @Holgerpoints out you can use the mapmethod instead of forEachlike this:

评论者@Holger指出您可以使用该map方法而不是forEach这样:

int[] result = IntStream.range(0, a.length).map(i -> a[i] * b[i]).toArray();

回答by Ian Knight

You can turn an array into a stream by using Arrays.stream():

您可以使用以下命令将数组转换为流Arrays.stream()

int[] ns = new int[] {1,2,3,4,5};
Arrays.stream(ns);

Once you've got your stream, you can use any of the methods described in the documentation, like sum()or whatever. You can mapor filterlike in Python by calling the relevant stream methods with a Lambda function:

获得流后,您可以使用文档中描述的任何方法,例如sum()或其他。您可以mapfilter喜欢在 Python 中通过使用 Lambda 函数调用相关的流方法:

Arrays.stream(ns).map(n -> n * 2);
Arrays.stream(ns).filter(n -> n % 4 == 0);

Once you're done modifying your stream, you then call toArray()to convert it back into an array to use elsewhere:

完成对流的修改后,您可以调用toArray()将其转换回数组以在其他地方使用:

int[] ns = new int[] {1,2,3,4,5};
int[] ms = Arrays.stream(ns).map(n -> n * 2).filter(n -> n % 4 == 0).toArray();

回答by Sanghyun Lee

Be careful if you have to deal with large numbers.

如果您必须处理大量数字,请务必小心。

int[] arr = new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE};
long sum = Arrays.stream(arr).sum(); // Wrong: sum == 0

The sum above is not 2 * Integer.MIN_VALUE. You need to do this in this case.

上面的总和不是2 * Integer.MIN_VALUE。在这种情况下,您需要这样做。

long sum = Arrays.stream(arr).mapToLong(Long::valueOf).sum(); // Correct

回答by Rafael

Please note that Arrays.stream(arr) create a LongStream (or IntStream, ...) instead of Stream so the map function cannot be used to modify the type. This is why .mapToLong, mapToObject, ... functions are provided.

请注意,Arrays.stream(arr) 创建的是 LongStream(或 IntStream,...)而不是 Stream,因此不能使用 map 函数来修改类型。这就是提供 .mapToLong、mapToObject、... 函数的原因。

Take a look at why-cant-i-map-integers-to-strings-when-streaming-from-an-array

看一下为什么不能从数组中流式传输整数到字符串时