ArrayList容器排序 comparator介面與comparable介面的使用
阿新 • • 發佈:2019-01-02
1、實體類實現comparable介面,重寫compareTo方法
package io; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Student implements Comparable<Student>{ private String name; private int age; private Date birthDay; public Student(String name, int age, String birthDay) { super(); this.name = name; this.age = age; DateFormat ds=new SimpleDateFormat("yyyy-MM-dd"); try { this.birthDay = ds.parse(birthDay); } catch (ParseException e) { e.printStackTrace(); }; } 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; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } //按名字降序 按生日升序 @Override public int compareTo(Student o) { int result=0; result=-this.name.compareTo(o.name); if(result==0){ result=this.birthDay.compareTo(o.birthDay); } return result; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", birthDay=" + birthDay + "]"; } }
package io; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class App { public static void main(String[] args) { List<Student> list=new ArrayList<Student>(); list.add(new Student("張三", 20, "1987-10-5")); list.add(new Student("王五", 22, "1986-10-5")); list.add(new Student("趙六", 25, "1988-10-5")); list.add(new Student("王七", 19, "1980-10-5")); Collections.sort(list); for(Student s:list){ System.out.println(s); } } }
2、建立新的業務類實現comparator介面,重寫compare方法。
package io; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Student { private String name; private int age; private Date birthDay; public Student(String name, int age, String birthDay) { super(); this.name = name; this.age = age; DateFormat ds=new SimpleDateFormat("yyyy-MM-dd"); try { this.birthDay = ds.parse(birthDay); } catch (ParseException e) { e.printStackTrace(); }; } 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; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ", birthDay=" + birthDay + "]"; } }
package io;
import java.util.Comparator;
public class BirthdayComp implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.getBirthDay().compareTo(o2.getBirthDay());
}
}
package io;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class App {
public static void main(String[] args) {
List<Student> list=new ArrayList<Student>();
list.add(new Student("張三", 20, "1987-10-5"));
list.add(new Student("王五", 22, "1986-10-5"));
list.add(new Student("趙六", 25, "1988-10-5"));
list.add(new Student("王七", 19, "1980-10-5"));
Collections.sort(list, new BirthdayComp());
for(Student s:list){
System.out.println(s);
}
}
}