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);
}
}
🐞标题:list 常见的统计(求和、最大、最小、平均)
👽作者:ruige
🐾地址:https://jjdhhc.com/articles/2020/11/07/1604720568091.html
🙏感恩:谢谢您的打赏与支持!中间图片是我的微信公众号,扫码关注哦!