JAVA8 List最大值最小值求和平均值以及排序
2023-03-15 22:06:09 时间
1:对象类型获取最大值、最小值、平均数
public static void main(String[] args) {
List<User> uList=new ArrayList<User>();
uList.add(new User(1, "xxx", 1, 18));
uList.add(new User(2, "zzz", 1, 19));
uList.add(new User(3, "aaa", 1, 20));
uList.add(new User(4, "bbb", 1, 21));
//最大年龄
Integer maxAge=uList.stream().mapToInt(User::getAge).max().getAsInt();
System.out.println("最大年龄为:"+maxAge);
//最小年龄
Integer minAge=uList.stream().mapToInt(User::getAge).min().getAsInt();
System.out.println("最小年龄为:"+minAge);
//年龄和
Integer sumAge=uList.stream().mapToInt(User::getAge).sum();
System.out.println("年龄和为:"+sumAge);
//年龄平均值
double avgAge=uList.stream().mapToDouble(User::getAge).average().getAsDouble();
System.out.println("年龄平均值为:"+avgAge);
double[] d={110.12,12.3,110.23,78.9};
double minDouble=Arrays.stream(d).min().getAsDouble();
System.out.println("最小数组值:"+minDouble);
}
2:number类型获取最大值、最小值、平均数
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Double(123.23));
list.add(new Double(33.23));
list.add(new Double(13.23));
list.add(new Double(3.23));
System.out.println(list);
System.out.println("最大值: " + Collections.max(list));
System.out.println("最小值: " + Collections.min(list));
List<Integer> lists = list;
DoubleSummaryStatistics statistics = lists.stream().mapToDouble(Number::doubleValue).summaryStatistics();
System.out.println("最大值:" + statistics.getMax());
System.out.println("最小值:" + statistics.getMin());
System.out.println("平均值:" + statistics.getAverage());
System.out.println("平均值四舍五入:" + Math.round(statistics.getAverage()));
System.out.println("求和:" + statistics.getSum());
System.out.println("计数:" + statistics.getCount());
}
3:对对象类型数据的list做排序
//测试数据,请不要纠结数据的严谨性
List<StudentInfo> studentList = new ArrayList<>();
studentList.add(new StudentInfo("李小明",true,18));
studentList.add(new StudentInfo("张小丽",false,21));
studentList.add(new StudentInfo("王大朋",true,19));
studentList.add(new StudentInfo("陈小跑",false,17));
//排序前输出
StudentInfo.printStudents(studentList);
//按年龄排序(Integer类型) 使用年龄进行升序排序
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
//排序后输出
StudentInfo.printStudents(studentsSortName);
//排序前输出
StudentInfo.printStudents(studentList);
//按年龄排序(Integer类型) 使用年龄进行降序排序(使用reversed()方法)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());
//排序后输出
StudentInfo.printStudents(studentsSortName);
//排序前输出
StudentInfo.printStudents(studentList);
//按年龄排序(Integer类型) 使用年龄进行降序排序,年龄相同再使用身高升序排序
List<StudentInfo> studentsSortName = studentList.stream()
.sorted(Comparator.comparing(StudentInfo::getAge).reversed().thenComparing(StudentInfo::getHeight))
.collect(Collectors.toList());
//排序后输出
StudentInfo.printStudents(studentsSortName);
相关文章
- 金融服务领域的大数据:即时分析
- 影响大数据、机器学习和人工智能未来发展的8个因素
- 从0开始构建一个属于你自己的PHP框架
- 如何将Hadoop集成到工作流程中?这6个优秀实践必看
- SEO公司使用大数据优化其模型的5种方法
- 关于Web Workers你需要了解的七件事
- 深入理解HTTPS原理、过程与实践
- 增强分析:数据和分析的未来
- PHP协程实现过程详解
- AI专家:大数据知识图谱——实战经验总结
- 关于PHP的错误机制总结
- 利用数据分析量化协同过滤算法的两大常见难题
- 怎么做大数据工作流调度系统?大厂架构师一语点破!
- 2019大数据处理必备的十大工具,从Linux到架构师必修
- OpenCV中的KMeans算法介绍与应用
- 教大家如果搭建一套phpstorm+wamp+xdebug调试PHP的环境
- CentOS下三种PHP拓展安装方法
- Go语言HTTP Server源码分析
- Go语言HTTP Server源码分析
- 2017年4月编程语言排行榜:Hack首次进入前五十