java中list集合按对象属性进⾏排序
在⽇常搬砖中,我们可能会需要对List中⾃定义的⼀些对象进⾏排序,但java是不知道我们的对象是需要怎么排序,因此我们得⾃⼰写排序的规则。
jdk提供了两个对象⽐较的接⼝Comparable和Comparator,通过实现接⼝可以对两个或多个对象进⾏⽐较,确认它们的⼤⼩关系或排列顺序。
下⾯假如有⼀个业务报表需求:需要按对象的时间字段optime倒序排列,假设数据库中返回的数据是乱序,为了减少查询时间不使⽤sql排序,在程序中进⾏排序。
⼀、实现Comparable接⼝
需要排序对象的类实现Comparable接⼝重写compareTo⽅法。
这种⽅式需要对原来的类上进⾏修改。
Comparable可以理解为,原始对象类实现了Comparable接⼝有了⽐较的能⼒,你给我⼀个对象我就可以和它⽐较。
报表类Report:
public class Report implements Comparable<Report> {
private String account;
private String opetime;
private String channel;
private BigDecimal amount;
private String nodeno;
private String nodetag;
public Report(String account, String opetime, String channel, BigDecimal amount, String nodeno, String nodetag) {
this.account = account;
this.opetime = opetime;
this.channel = channel;
this.amount = amount;
}
// getter和sertter省略...
@Override
public int compareTo(Report o) {
// 格式化时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt1 = null;
Date dt2 = null;
try {
dt1 = sdf.parse(getOpetime());
dt2 = sdf.Opetime());
} catch (ParseException e) {
e.printStackTrace();
}
// ⽤时间字段进⾏⽐较
if (dt1pareTo(dt2) > 0) {
return -1; // 交换-1和1的位置就可以控制正序和倒序
}else if (dt1pareTo(dt2) < 0){
return 1;
}else{
return 0;
}
}
测试:
⽤Collections类中的sort⽅法对List进⾏排序。
// 模拟数据库中返回的数据
ArrayList<Report> reports = new ArrayList<>();
reports.add(new Report("50030001", "2021-05-23 19:08:26", "1004",new BigDecimal(103),"50000001","亚洲⽹点"));
reports.add(new Report("50030231", "2021-04-23 11:08:26", "1004",new BigDecimal(20),"50000001","北欧洲⽹点"));
reports.add(new Report("50034341", "2021-03-23 14:08:26", "1005",new BigDecimal(90),"50000001","⾮洲⽹点"));
reports.add(new Report("50034341", "2021-02-23 14:08:26", "1005",new BigDecimal(90),"50000001","⾮洲⽹点"));
reports.add(new Report("50036547", "2021-06-23 12:08:26", "1004",new BigDecimal(88),"50000001","美洲⽹点"));
reports.add(new Report("50033698", "2021-01-23 08:08:26", "1003",new BigDecimal(1000),"50000001","711⽹点"));
System.out.String());
Collections.sort(reports); // 对象⾃⼰有⽐较的能⼒不需要传⽐较器
System.out.println("--------------------------------------------排序前后分割线-----------------------------------------------------");
System.out.String());
输出结果:
⼆、实现Comparator接⼝
如果不想在原有的类上进⾏修改,那么可以单独写⼀个⽐较器,⽐较器类需要实现Comparator接⼝并重写compare⽅法,⽐较⽅法和compareTo差不多,但需要传输两个对象进⾏⽐较。
Comparator可以理解为,原始对象不会⽐较,我通过创建⼀个第三⽅的⽐较器强制对它们进⾏⽐较。
/java集合排序怎么实现
ReportComparator⽐较器:
public class ReportComparator implements Comparator<Report> {
@Override
public int compare(Report o1, Report o2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt1 = null;
Date dt2 = null;
try {
dt1 = sdf.Opetime());
dt2 = sdf.Opetime());
} catch (ParseException e) {
e.printStackTrace();
}
if (dt1pareTo(dt2) > 0) {
return -1;
}else if (dt1pareTo(dt2) < 0){
return 1;
}else{
return 0;
}
}
}
测试:
public class DateTest {
public static void main(String[] args) throws ParseException {
// 模拟数据库中返回的数据
ArrayList<Report> reports = new ArrayList<>();
reports.add(new Report("50030001", "2021-05-23 19:08:26", "1004",new BigDecimal(103),"50000001","亚洲⽹点"));
reports.add(new Report("50030231", "2021-04-23 11:08:26", "1004",new BigDecimal(20),"50000001","北欧洲⽹点"));
reports.add(new Report("50034341", "2021-03-23 14:08:26", "1005",new BigDecimal(90),"50000001","⾮洲⽹点"));
reports.add(new Report("50034341", "2021-02-23 14:08:26", "1005",new BigDecimal(90),"50000001","⾮洲⽹点"));
reports.add(new Report("50036547", "2021-06-23 12:08:26", "1004",new BigDecimal(88),"50000001","美洲⽹点"));
reports.add(new Report("50033698", "2021-01-23 08:08:26", "1003",new BigDecimal(1000),"50000001","711⽹点"));
System.out.String());
Collections.sort(reports, new ReportComparator()); // 传⼊⼀个⽐较器,这个⽐较器也可以写匿名内部类实现
System.out.println("--------------------------------------------排序前后分割线-----------------------------------------------------");
System.out.String());
}
}
输出结果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。