java开发jdk1.7、jdk1.8下List集合中按照对象中指定字段排序,List集合中按照map中指定字段排序
2021/11/16 14:40:23
本文主要是介绍java开发jdk1.7、jdk1.8下List集合中按照对象中指定字段排序,List集合中按照map中指定字段排序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
jdk1.7List集合中按照对象中指定字段排序
题目:现在有一个list集合、里面存放着多个Student对象,现要求按照Student的score进行排序
代码演示:
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Student { public Student(String name, int score) { this.name = name; this.score = score; } private String name; private int score; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public static void main(String[] args) { Student student = new Student("张三",60); Student student2 = new Student("李四",30); Student student3 = new Student("王五",80); Student student4 = new Student("赵六",90); List<Student> studentList = new ArrayList<>(); studentList.add(student); studentList.add(student2); studentList.add(student3); studentList.add(student4); System.out.println("排序前"); for (Student o : studentList) { System.out.println(o.getName()+" "+o.getScore()); } Collections.sort(studentList, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { //排序字段 正序 如果需要倒序则将两个值调换 return o1.getScore()-o2.getScore(); } }); System.out.println("排序后"); for (Student o : studentList) { System.out.println(o.getName()+" "+o.getScore()); } } }
结果输出:
jdk1.7List集合中按照map中指定字段排序
题目:现在有一个list集合、里面存放着多个Map,Map中用于存储学生的姓名,语文成绩、数学成绩、英语成绩,现在需要按照语文成绩进行排序输出
代码演示:
import java.util.*; public class Student { public static void main(String[] args) { List<Map> studentList = new ArrayList(); Map map = new HashMap();map.put("姓名","张三");map.put("语文",60);map.put("数学",65);map.put("英语",70); studentList.add(map); map = new HashMap();map.put("姓名","李四");map.put("语文",80);map.put("数学",40);map.put("英语",50); studentList.add(map); map = new HashMap();map.put("姓名","王五");map.put("语文",70);map.put("数学",90);map.put("英语",30); studentList.add(map); System.out.println("排序前"); for (Map o : studentList) { System.out.println(o.get("姓名")+" "+o.get("语文")+" "+o.get("数学")+" "+o.get("英语")); } Collections.sort(studentList, new Comparator<Map>() { @Override public int compare(Map o1, Map o2) { //排序字段 正序 如果需要倒序则将两个值调换 return Integer.parseInt(o1.get("语文").toString())-Integer.parseInt(o2.get("语文").toString()); } }); System.out.println("排序后"); for (Map o : studentList) { System.out.println(o.get("姓名")+" "+o.get("语文")+" "+o.get("数学")+" "+o.get("英语")); } } }
结果输出:
jdk1.8List集合中按照对象中指定字段排序
这篇关于java开发jdk1.7、jdk1.8下List集合中按照对象中指定字段排序,List集合中按照map中指定字段排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南