先看图:
假如你有一个List集合对象,每个对象中有一个属性是数量,你现在想要把他们相加在一起,算出和,利用下面这个方法,绝对好使!
//对象格式,nowSaveData是个集合对象,例如List<User>
IntSummaryStatistics intSummaryStatistics = nowSaveData.stream().mapToInt((x) -> Math.toIntExact(x.getDefectCount())).summaryStatistics();
//例如List<int> list = new ArrayList<>();
IntSummaryStatistics intSummaryStatistics = list .stream().mapToInt((x) -> x)).summaryStatistics();
这是个对象,我们直接看返回的啥东西