如何使用流映射更新 ArrayList of Integers 的 ArrayList 中的元素并减少?
How to update elements inside an ArrayList of ArrayLists of Integers using stream map and reduce?
基本上,我有一个ArrayList<ArrayList<Integer>> = [[-1,-4], [3,2], [0,-4], [-3,2]
。我想使用流遍历每个 ArrayList 并将元素添加在一起,如下所示:[[-5],[5],[-4], [-1]]
。然后我想将它转换成一个 int[] 数组并以这样的结尾:[-5, 5, -4, -1]
.
到目前为止我有这个:
ArrayList<ArrayList<Integer>> outerNums = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> innerNums0 = new ArrayList<>();
ArrayList<Integer> innerNums1 = new ArrayList<>();
ArrayList<Integer> innerNums2 = new ArrayList<>();
ArrayList<Integer> innerNums3 = new ArrayList<>();
innerNums0.add(-1);
innerNums0.add(-4);
innerNums1.add(3);
innerNums1.add(2);
innerNums2.add(0);
innerNums2.add(-4);
innerNums3.add(-3);
innerNums3.add(2);
//
outerNums.add(innerNums0);
outerNums.add(innerNums1);
outerNums.add(innerNums2);
outerNums.add(innerNums3);
System.out.println(res(outerNums));
}
public static int[] res(ArrayList<ArrayList<Integer>> innerNums) {
System.out.println(innerNums);
//Need to use both map and reduce
int[] result = innerNums.stream().map().reduce(); //not sure what to do here.
return result;
}
对于每个内部列表,在将总和收集到数组之前对其进行流式处理和求和。
int[] result = outerNums.stream()
.mapToInt(innerArrayList -> innerArrayList.stream()
.mapToInt(i -> i)
.sum())
.toArray();
注意:mapToInt
returns 一个 IntStream
。
您可以使用 IntStream#sum
.
innerNums.stream().mapToInt(x -> x.stream().mapToInt(i -> i).sum()).toArray();
我想这就是您要找的东西?
List<List<Integer>> list = List.of(List.of(-1,-4), List.of(3,2), List.of(0,-4), List.of(-3,2));
int[] result = list.stream()
.map(innerList -> innerList.stream().reduce(0, Integer::sum))
.mapToInt(Integer::intValue)
.toArray();
System.out.println(Arrays.toString(result));
输出
[-5, 5, -4, -1]
基本上,我有一个ArrayList<ArrayList<Integer>> = [[-1,-4], [3,2], [0,-4], [-3,2]
。我想使用流遍历每个 ArrayList 并将元素添加在一起,如下所示:[[-5],[5],[-4], [-1]]
。然后我想将它转换成一个 int[] 数组并以这样的结尾:[-5, 5, -4, -1]
.
到目前为止我有这个:
ArrayList<ArrayList<Integer>> outerNums = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> innerNums0 = new ArrayList<>();
ArrayList<Integer> innerNums1 = new ArrayList<>();
ArrayList<Integer> innerNums2 = new ArrayList<>();
ArrayList<Integer> innerNums3 = new ArrayList<>();
innerNums0.add(-1);
innerNums0.add(-4);
innerNums1.add(3);
innerNums1.add(2);
innerNums2.add(0);
innerNums2.add(-4);
innerNums3.add(-3);
innerNums3.add(2);
//
outerNums.add(innerNums0);
outerNums.add(innerNums1);
outerNums.add(innerNums2);
outerNums.add(innerNums3);
System.out.println(res(outerNums));
}
public static int[] res(ArrayList<ArrayList<Integer>> innerNums) {
System.out.println(innerNums);
//Need to use both map and reduce
int[] result = innerNums.stream().map().reduce(); //not sure what to do here.
return result;
}
对于每个内部列表,在将总和收集到数组之前对其进行流式处理和求和。
int[] result = outerNums.stream()
.mapToInt(innerArrayList -> innerArrayList.stream()
.mapToInt(i -> i)
.sum())
.toArray();
注意:mapToInt
returns 一个 IntStream
。
您可以使用 IntStream#sum
.
innerNums.stream().mapToInt(x -> x.stream().mapToInt(i -> i).sum()).toArray();
我想这就是您要找的东西?
List<List<Integer>> list = List.of(List.of(-1,-4), List.of(3,2), List.of(0,-4), List.of(-3,2));
int[] result = list.stream()
.map(innerList -> innerList.stream().reduce(0, Integer::sum))
.mapToInt(Integer::intValue)
.toArray();
System.out.println(Arrays.toString(result));
输出
[-5, 5, -4, -1]