List<User> userlist = userService.findAll();
Integer sum= userlist .stream().collect(Collectors.summingInt(User::getAge));
package com.example.list_test;
import java.util.ArrayList;
import java.util.List;
/**
* 描述:ListTest3
*
* @author 何志鹏
* @ClassName:ListTest3
* @create 2019-05-30 18:49
* Version 1.0
*/
public class ListTest3 {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s1 = new Student();
s1.setId(1);
s1.setName("张三");
s1.setAge(28);
Student s2 = new Student();
s2.setId(2);
s2.setName("李四");
s2.setAge(29);
Student s3 = new Student();
s3.setId(3);
s3.setName("网二");
s3.setAge(30);
list.add(s1);
list.add(s2);
list.add(s3);
System.out.println("=========第一种方式================");
int suma = list.stream().map(e -> e.getAge()).reduce(Integer::sum).get();//求和
System.out.println(suma);
int maxa = list.stream().map(e -> e.getAge()).reduce(Integer::max).get();//最大
System.out.println(maxa);
int mina = list.stream().map(e -> e.getAge()).reduce(Integer::min).get();//最小
System.out.println(mina);
System.out.println("=========第二种方式================");
double doublesum = list.stream().mapToDouble(Student::getAge).sum();//和
System.out.println(doublesum);
int intmax = list.stream().mapToInt(Student::getAge).max().getAsInt();//最大
System.out.println(intmax);
int intmin = list.stream().mapToInt(Student::getAge).min().getAsInt();//最小
System.out.println(intmin);
double avg = list.stream().mapToDouble(Student::getAge).average().getAsDouble();//平均
System.out.println(avg);
}
}
————————————————
版权声明:本文为CSDN博主「浅唱~幸福」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39643007/article/details/90703329