java 8将两个int数组的所有值相加。

时间:2021-10-18 08:35:28
int[] ar1 = {1,2,3};
int[] ar2 = {1,2,3};

Output:

输出:

{2,3,4,3,4,5,4,5,6}

I tried something like this:

我试过这样的方法:

IntStream.range(0,ar1.length).map(x -> IntStream.range(0,ar2.length).map(y -> ar1[y]+ar2[x]));

but this does not work:

但这行不通:

Type mismatch: cannot convert from IntStream to int.

类型不匹配:不能从IntStream转换为int。

How can I sum every value of ar1 with every value of ar2 in Java 8 using streams?

我如何使用流来对Java 8中的ar2的每个值求和?

3 个解决方案

#1


14  

You should use flatMap, since map of IntStream is expected to map each int element of the original IntStream to an int, not to an IntStream.

您应该使用flatMap,因为预期IntStream的映射会将原始IntStream的每个int元素映射到int,而不是IntStream。

System.out.println(Arrays.toString (
    IntStream.range(0,ar1.length)
             .flatMap(x -> IntStream.range(0,ar2.length).map(y -> ar1[x]+ar2[y]))
             .toArray ()));

Output:

输出:

[2, 3, 4, 3, 4, 5, 4, 5, 6]

As an alternative to creating an IntStream of the indices of the array, you can create an IntStream of the elements of the array:

作为创建数组索引的IntStream的备选方案,您可以创建数组元素的IntStream:

System.out.println(Arrays.toString (
    Arrays.stream(ar1)
          .flatMap(x -> Arrays.stream(ar2).map(y -> x + y))
          .toArray ()));

#2


8  

For each ith element, you can create a IntStream of sums i + j of this element and a jth element of another array:

对于每个第i个元素,您可以创建该元素的i + j和另一个数组的第j个元素的i + j的IntStream:

IntStream.of(ar1).flatMap(i -> IntStream.of(ar2).map(j -> i + j)).toArray();

I would not recommend using streams of indexes (IntStream.range(0,ar1.length)) where they can be replaced with streams of values themselves (IntStream.of(ar1)).

我不建议使用索引流(IntStream.range(0,ar1.length)),在那里可以用价值流替换它们(IntStream.of(ar1))。

#3


6  

Beside that of the IntStream#flatMap, you can use the Arrays#stream(int[]) to create an IntStream. for example:

在IntStream#flatMap的旁边,您可以使用数组#stream(int[])创建一个IntStream。例如:

int[] sum = Arrays.stream(ar1)
                  .flatMap(left -> Arrays.stream(ar2).map(right -> left + right))
                  .toArray();

System.out.println(Arrays.toString(sum));
//                        ^--- [2, 3, 4, 3, 4, 5, 4, 5, 6]

#1


14  

You should use flatMap, since map of IntStream is expected to map each int element of the original IntStream to an int, not to an IntStream.

您应该使用flatMap,因为预期IntStream的映射会将原始IntStream的每个int元素映射到int,而不是IntStream。

System.out.println(Arrays.toString (
    IntStream.range(0,ar1.length)
             .flatMap(x -> IntStream.range(0,ar2.length).map(y -> ar1[x]+ar2[y]))
             .toArray ()));

Output:

输出:

[2, 3, 4, 3, 4, 5, 4, 5, 6]

As an alternative to creating an IntStream of the indices of the array, you can create an IntStream of the elements of the array:

作为创建数组索引的IntStream的备选方案,您可以创建数组元素的IntStream:

System.out.println(Arrays.toString (
    Arrays.stream(ar1)
          .flatMap(x -> Arrays.stream(ar2).map(y -> x + y))
          .toArray ()));

#2


8  

For each ith element, you can create a IntStream of sums i + j of this element and a jth element of another array:

对于每个第i个元素,您可以创建该元素的i + j和另一个数组的第j个元素的i + j的IntStream:

IntStream.of(ar1).flatMap(i -> IntStream.of(ar2).map(j -> i + j)).toArray();

I would not recommend using streams of indexes (IntStream.range(0,ar1.length)) where they can be replaced with streams of values themselves (IntStream.of(ar1)).

我不建议使用索引流(IntStream.range(0,ar1.length)),在那里可以用价值流替换它们(IntStream.of(ar1))。

#3


6  

Beside that of the IntStream#flatMap, you can use the Arrays#stream(int[]) to create an IntStream. for example:

在IntStream#flatMap的旁边,您可以使用数组#stream(int[])创建一个IntStream。例如:

int[] sum = Arrays.stream(ar1)
                  .flatMap(left -> Arrays.stream(ar2).map(right -> left + right))
                  .toArray();

System.out.println(Arrays.toString(sum));
//                        ^--- [2, 3, 4, 3, 4, 5, 4, 5, 6]