Java对List进行排序的两种实现方法
作者:shangmingchao 时间:2022-10-31 13:40:06
前言
Java.util包中的List接口继承了Collection接口,用来存放对象集合,所以对这些对象进行排序的时候,要么让对象类自己实现同类对象的比较,要么借助比较器进行比较排序。
学生实体类,包含姓名和年龄属性,比较时先按姓名升序排序,如果姓名相同则按年龄升序排序。
第一种:实体类自己实现比较
(实现comparable接口:public interface Comparable<T>
,里面就一个方法声明:public int compareTo(T o);
)
示例代码:
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int flag = this.name.compareTo(o.name);
if(flag == 0) {
flag = this.age - o.age;
}
return flag;
}
}
然后利用List类的sort(Comparator<? super E> c)
方法或java.util.Collections
工具类的sort(List<T> list)
(其实里面就一句:list.sort(null);
)进行排序:
List<Student> students = new ArrayList<Student>();
students.add(new Student("a",10));
students.add(new Student("b",12));
students.add(new Student("b",11));
students.add(new Student("ac",20));
students.sort(null);
//Collections.sort(students);
结果:
a 10
ac 20
b 11
b 12
第二种:借助比较器进行排序。
示例代码:
public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
比较器java.util.Comparator
类是一个接口(public interface Comparator<T>
),包含int compare(T o1, T o2);
等方法:
我们的比较器要实现该接口并实现compare
方法:
private class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int flag = o1.getName().compareTo(o2.getName());
if(flag == 0) {
flag = o1.getAge() - o2.getAge();
}
return flag;
}
}
比较的时候可以利用List的sort(Comparator<? super E> c)
方法(或者java.util.Collections
工具类的sort(List<T> list, Comparator<? super T> c)
方法)进行排序。
List<Student> students = new ArrayList<Student>();
students.add(new Student("a",10));
students.add(new Student("b",12));
students.add(new Student("b",11));
students.add(new Student("ac",20));
Test t = new Test();
students.sort(t.new StudentComparator());
//Collections.sort(students, t.new StudentComparator());
for(Student student : students) {
System.out.println(student.getName()+" "+student.getAge());
}
结果跟第一种方法一样:
a 10
ac 20
b 11
b 12
来源:http://blog.csdn.net/shangmingchao/article/details/47145569
标签:java,list,排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现TopK问题的方法
2023-11-10 20:32:14
Mybatis拦 截 器实现数据权限的示例代码
2023-11-19 20:15:04
spring boot 实现配置多个DispatcherServlet最简单方式
2023-12-18 03:24:46
![](https://img.aspxhome.com/file/2023/9/63769_0s.jpg)
Springboot @Validated和@Valid的区别及使用详解
2023-05-30 18:40:25
![](https://img.aspxhome.com/file/2023/6/64916_0s.png)
mybatis 多表关联mapper文件写法操作
2021-12-02 23:21:25
Java 根据网址查询DNS/IP地址的方法
2023-06-21 15:31:54
Jersey Restful接口如何获取参数的问题
2023-10-29 14:44:16
关于Jsoup将相对路径转为绝对路径的方法
2022-03-11 04:13:50
![](https://img.aspxhome.com/file/2023/4/63844_0s.png)
Mybatis常用分页插件实现快速分页处理技巧
2022-12-25 00:03:45
Java创建线程池为什么一定要用ThreadPoolExecutor
2023-04-22 06:03:31
![](https://img.aspxhome.com/file/2023/1/64391_0s.webp)
解决mybatis update并非所有字段需要更新问题
2022-12-09 10:20:55
![](https://img.aspxhome.com/file/2023/9/61599_0s.jpg)
ArrayList详解和使用示例_动力节点Java学院整理
2023-08-05 13:15:41
![](https://img.aspxhome.com/file/2023/0/57840_0s.png)
分析并发编程之LongAdder原理
2023-05-11 17:19:30
![](https://img.aspxhome.com/file/2023/9/65599_0s.jpg)
JavaMail入门教程之解析邮件(5)
2023-11-29 08:25:13
![](https://img.aspxhome.com/file/2023/0/60030_0s.jpg)
SpringBoot整合Shiro框架,实现用户权限管理
2021-10-27 00:03:02
SpringBoot文件上传大小设置方式(yml中配置)
2021-09-21 09:26:47
Android自定义TextView跑马灯效果
2023-08-07 01:14:57
浅谈mybatis中SQL语句给boolean类型赋值问题
2023-01-19 15:15:42
Java可变个数形参的方法实例代码
2023-01-15 18:35:56
![](https://img.aspxhome.com/file/2023/2/62372_0s.png)
SpringBoot集成cache缓存的实现
2023-11-27 16:37:39
![](https://img.aspxhome.com/file/2023/7/65327_0s.png)