java8 - >五种求和方式
/*** 五种求和方式
* @Author: Mr.li
* @Date: 2020/9/8
*/
@Test
public void demo3(){
List<Person> personlist = new ArrayList<>();
Person person = new Person();
for(int i=0;i<10;i++){
person.setId(i);
person.setName("你好=="+i);
personlist.add(person);
}
Long firstSum = personlist.stream()
.collect(Collectors.summarizingLong(Person::getId)).getSum();
Long secondSum = personlist.stream()
.collect(Collectors.summingLong(Person::getId));
int thirdSum = personlist.stream()
.mapToInt(Person::getId).sum();
Integer forthSum = personlist.stream()
.collect(Collectors.reducing(0,Person::getId,(i,j)->i+j));
IntSummaryStatistics stats = personlist.stream().mapToInt(Person::getId).summaryStatistics();
Long fiveSum = stats.getSum();
System.out.println(firstSum + ":" + secondSum + ":" + thirdSum + ":" + forthSum + ":" + fiveSum);
}
redult:
以上是 java8 - >五种求和方式 的全部内容, 来源链接: utcz.com/z/394761.html