相关文章推荐
帅气的刺猬  ·  将字符串数组转换为React中的对象数组-腾 ...·  2 年前    · 
要出家的鞭炮  ·  [已完结][论文汇总] CVPR 2023 ...·  2 年前    · 
强悍的楼梯  ·  jsp文件过大,is exceeding ...·  2 年前    · 
坚韧的南瓜  ·  jquery关闭页面事件-掘金·  2 年前    · 
讲道义的机器猫  ·  远程终端神器-tmux - 掘金·  2 年前    · 
Code  ›  JAVA8 List最大值最小值求和平均值以及排序开发者社区
list list排序
https://cloud.tencent.com/developer/article/1903302
焦虑的皮带
2 年前
作者头像
用户1134008
0 篇文章

JAVA8 List最大值最小值求和平均值以及排序

原创
前往专栏
腾讯云
备案 控制台
开发者社区
学习
实践
活动
专区
工具
TVP
文章/答案/技术大牛
写文章
社区首页 > 专栏 > java 8 > 正文

JAVA8 List最大值最小值求和平均值以及排序

原创
修改 于 2021-11-19 18:12:35
661 1
举报

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类型)		使用年龄进行降序排序,年龄相同再使用身高升序排序
 
推荐文章
帅气的刺猬  ·  将字符串数组转换为React中的对象数组-腾讯云开发者社区-腾讯云
2 年前
要出家的鞭炮  ·  [已完结][论文汇总] CVPR 2023 对抗样本方向论文整理 - 知乎
2 年前
强悍的楼梯  ·  jsp文件过大,is exceeding 65535 bytes limit - H.U.C-王子 - 博客园
2 年前
坚韧的南瓜  ·  jquery关闭页面事件-掘金
2 年前
讲道义的机器猫  ·  远程终端神器-tmux - 掘金
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号