英文:
In Java, how to find the sum of several fields in a list of objects using stream API?
问题
以下是已翻译的代码部分:
List<Object> objectList = new ArrayList<>();
objectList.add(new Object(X1="ABC", X2=1, X3=3));
objectList.add(new Object(X1="XYZ", X2=2, X3=7));
objectList add(new Object(X1="AC", X2=1, X3=3));
objectList add(new Object(X1="AB", X2=1, X3=3));
int X2Sum = objectList.stream().mapToInt(o -> o.getX2()).sum();
int X3Sum = objectList.stream().mapToInt(o -> o.getX3()).sum();
英文:
As shown below, I have a list of objects with integer fields like X2 and X3.
List<Object> objectList=new ArrayList<>();
objectList.add(new Object(X1="ABC",X2=1,X3=3));
objectList.add(new Object(X1="XYZ",X2=2,X3=7));
objectList.add(new Object(X1="AC",X2=1,X3=3));
objectList.add(new Object(X1="AB",X2=1,X3=3));
The goal is to navigate through this list and get the sum of all X2s and X3s.
I attempted the method below to calculate the sum, but I believe there should be a way to do it with one stream() call.
int X2Sum=objectList.stream().mapToInt(o->o.getX2()).sum();
int X3Sum=objectList.stream().mapToInt(o->o.getX3()).sum();
答案1
得分: 2
IntStream.sum()
终端只返回流的一个单一总和。
这意味着您需要两个单独的流来获取两个总和,就像您已经有的那样:
int sumOfX2 = objectList.stream().mapToInt(o -> o.getX2()).sum();
int sumOfX3 = objectList.stream().mapToInt(o -> o.getX3()).sum();
只使用一个流的替代方法需要一个 reduce
步骤,如 https://stackoverflow.com/questions/50582475/reduce-operation-on-custom-object-in-java 中所解释的。
List<Pojo> pojoList = new ArrayList<>();
pojoList.add(new Pojo("ABC", 1, 3));
pojoList add(new Pojo("XYZ", 2, 7));
var pojoSum = pojoList.stream().reduce(
new Pojo("sumX", 0, 0), // 初始结果
// 组合器已经是累加器
(pojoResult, pojoToAdd) -> {
pojoResult.setX2(pojoResult.getX2() + pojoToAdd.getX2());
pojoResult.setX3(pojoResult.getX3() + pojoToAdd.getX3());
return pojoResult;
}
);
参见:
https://www.baeldung.com/java-stream-sum
英文:
The IntStream.sum()
terminal only returns one single sum per stream.
This means you need two separate streams for two sums like you already have:
int sumOfX2 = objectList.stream().mapToInt(o->o.getX2()).sum();
int sumOfX3 = objectList.stream().mapToInt(o->o.getX3()).sum();
An alternative way with only using one stream would require a reduce
step as explained in https://stackoverflow.com/questions/50582475/reduce-operation-on-custom-object-in-java.
List<Pojo> pojoList = new ArrayList<>();
pojoList.add(new Pojo("ABC", 1, 3));
pojoList.add(new Pojo("XYZ", 2, 7));
var pojoSum = pojoList.stream().reduce(
new Pojo(¨sumX¨ 0,0), // initial result
// the combiner is already the accumulator
(pojoResult, pojoToAdd) -> {
pojoResult.setX2( pojoResult.getX2() + pojoToAdd.getX2() );
pojoResult.setX3( pojoResult.getX3() + pojoToAdd.getX3() );
return pojoResult;
}
);
See also:
https://www.baeldung.com/java-stream-sum
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论