List集合实现自定义排序
来源:互联网 发布:win10开机windows聚焦 编辑:程序博客网 时间:2024/06/05 14:30
List自定义排序根据Collections.sort重载方法来实现,实现Comparable接口。
实现Comparator ,重写compare方法。
public class DataSort implements Comparator<Student>{/** * 重写方法 实现自定义排序 */@Overridepublic int compare(Student o1, Student o2) {int i = o1.getAge() - o2.getAge();//按照年龄排序(从小到大) //年龄相等的情况下if(i == 0){ return 0; } return i; }public static void main(String[] args) {List<Student> students = new ArrayList<Student>(); students.add(new Student(11)); students.add(new Student(21)); students.add(new Student(31)); students.add(new Student(22)); students.add(new Student(15)); students.add(new Student(27)); for(Student stu : students) { System.out.println("age:" + stu.getAge()); } Collections.sort(students,new DataSort()); //排序 for(Student stu : students) { System.out.println("age:" + stu.getAge()); } }}
student类:
public class Student { private int age; public Student(int age) {super();this.age = age;}public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
0 0
- List集合实现自定义排序
- Lambda表达式实现List集合的自定义排序
- 实现List<>自定义排序
- Java实现List集合排序
- 使用Comparable、Comparator接口实现对对象数组、List集合自定义排序
- List集合排序(默认及自定义排序)
- 使用:Comparison委托,为list等集合做自定义排序。
- 对自定义对象集合List中的数据进行排序
- 对List集合中的对象进行自定义排序
- List集合内排序
- List集合排序
- list 集合对象 排序
- list集合排序
- List集合的排序
- 针对list集合排序
- List集合排序
- List集合排序
- list集合进行排序
- RabbitMQ从入门到精通----环境安装
- PathMeasure
- android的Notification折叠
- python manage.py syncdb Unknown command: 'syncdb'问题解决方法
- MySql- Access denied for user 'mysql用户名'@'主机或IP' (using password: YES)'
- List集合实现自定义排序
- 如何在CentOS 7上安装MySQL
- Event Hubs Receiver Epoch
- CUDA总结:同步
- java读取excel时间不对的问题
- xUtils3中对Sqlite数据库的操作
- FindObjectOfType用法
- 浅谈 &0xFF操作
- lunux卸载mysql