Java List获取集合中的最大值或最小值

您所在的位置:网站首页 字符串最大值和最小值怎么算的 Java List获取集合中的最大值或最小值

Java List获取集合中的最大值或最小值

2023-11-19 04:01| 来源: 网络整理| 查看: 265

1.List集合中的元素为基本类型,我们可以使用java.util中的Collections提供的方法,来取出最大值或者最小值。 (1)Integer类型

package com.yuxuange.study.test; import java.util.*; public class Test { public static void main(String[] args) { List list = new LinkedList(); list.add(99); list.add(54); list.add(82); list.add(12); list.add(33); list.add(79); list.add(90); list.add(7); list.add(66); list.add(38); list.add(7); System.out.println(list.toString()); Integer max = Collections.max(list); System.out.println("集合中最大值为:" + max); Integer min = Collections.min(list); System.out.println("集合中最小值为:" + min); } }

输出的结果为: 整型输出结果 (2)Double类型

package com.yuxuange.study.test; import java.util.*; public class Test { public static void main(String[] args) { List list = new LinkedList(); list.add(97.42); list.add(83.555); list.add(99.8045); list.add(45.33); list.add(8.2006); list.add(54.333); list.add(72.9055); list.add(56.999997); list.add(20.9); list.add(6.099912); System.out.println(list.toString()); Double max = Collections.max(list); System.out.println("集合中最大值为:" + max); Double min = Collections.min(list); System.out.println("集合中最小值为:" + min); } }

输出结果为: Double类型输出结果 2.List集合中的元素为引用类型时 (1)String类型

package com.yuxuange.study.test; import java.util.*; public class Test { public static void main(String[] args) { List list = new LinkedList(); list.add("97.435"); list.add("92"); list.add("80.3455"); list.add("10.9833"); list.add("52"); list.add("35.663444"); list.add("79.0"); list.add("42"); list.add("83.482"); list.add("26.454448"); System.out.println(list.toString()); String max = Collections.max(list); System.out.println("集合中最大值为:" + max); String min = Collections.min(list); System.out.println("集合中最小值为:" + min); } }

String类型输出结果

(2)对象,比如有个对象集合,集合中保存了学生的对象信息,我们要根据学号,年龄等字段找出对应的最大值或者最小值

package com.yuxuange.study.test; import java.util.*; public class Test { public static void main(String[] args) { List list = new LinkedList(); Student zhangsan = new Student(99, "张三", 28, 96.0); Student lisi = new Student(82, "李四", 25, 89.0); Student wangwu = new Student(26, "王五", 23, 72.5); Student zhaoliu = new Student(68, "赵六", 30, 65.5); Student tianqi = new Student(8, "田七", 26, 100.0); Student fengba = new Student(47, "冯八", 21, 52.5); Student jinjiu = new Student(14, "金九", 27, 46.5); list.add(zhangsan); list.add(lisi); list.add(wangwu); list.add(zhaoliu); list.add(tianqi); list.add(fengba); list.add(jinjiu); System.out.println(list.toString()); Student noMax = list.stream().max(Comparator.comparing(Student::getNo)).get(); Student noMin = list.stream().min(Comparator.comparing(Student::getNo)).get(); System.out.println("集合中学号最大的学生信息为" + noMax); System.out.println("集合中学号最小的学生信息为" + noMin); Student ageMax = list.stream().max(Comparator.comparing(Student::getAge)).get(); Student ageMin = list.stream().min(Comparator.comparing(Student::getAge)).get(); System.out.println("集合中年龄最大的学生信息为" + ageMax); System.out.println("集合中年龄最小的学生信息为" + ageMin); } } class Student { private Integer no; private String name; private Integer age; private Double score; public Student(Integer no, String name, Integer age, Double score) { this.no = no; this.name = name; this.age = age; this.score = score; } public Integer getNo() { return no; } public void setNo(Integer no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getScore() { return score; } public void setScore(Double score) { this.score = score; } @Override public String toString() { return "Student{" + "no=" + no + ", name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } }

输出结果为: 对象类型输出结果



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3