java 集合中自定义对象的几种排序方法
1. 通过实现Comparable接口,来对集合中的自定义对象排序
代码:
import java.util.*;
//student类,并实现Comparable接口
class Student implements Comparable<Student>{
//姓名,成绩,年龄三个变量
private String name;
private int score;
private int age;
//构造方法
public Student() {}
public Student(String name, int score,int age) {
this.name = name;
this.score = score;
this.age = age;
}
//get set 方法
public String getName() {
return this.name;
}
public int getScore() {
return this.score;
}
public int getAge(){
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setScore(int score) {
this.score = score;
}
public void setAge(int age){
this.age = age;
}
//重写toString方法
public String toString(){
return "姓名:"+this.getName()+"\t成绩:"+this.getScore()+"\t年龄:"+this.getAge();
}
@Override
//实现Comparable的compareTo方法
public int compareTo(Student stu) {
// TODO Auto-generated method stub
return this.getScore()-stu.getScore();
}
}
public class SortList {
public static void main(String [] args){
//集合的定义
List<Student> list = new ArrayList<Student>();
//学生对象的加入
list.add(new Student("张三",89,20));
list.add(new Student("李四",60,21));
list.add(new Student("路人",99,15));
//排序
Collections.sort(list);
//遍历输出
for(Student stu : list){
System.out.println(stu.toString());
}
}
}
运行结果:
2.通过实现Comparator接口,来对集合中的自定义对象排序,该方式相对于上一种方式优势在于可以按多种规则排序
代码:
import java.util.*;
//student类,
class Student {
//姓名,成绩,年龄三个变量
private String name;
private int score;
private int age;
//构造方法
public Student() {}
public Student(String name, int score,int age) {
this.name = name;
this.score = score;
this.age = age;
}
//get set 方法
public String getName() {
return this.name;
}
public int getScore() {
return this.score;
}
public int getAge(){
return this.age;
}
public void setName(String name) {
this.name = name;
}
public void setScore(int score) {
this.score = score;
}
public void setAge(int age){
this.age = age;
}
//重写toString方法
public String toString(){
return "姓名:"+this.getName()+"\t成绩:"+this.getScore()+"\t年龄:"+this.getAge();
}
}
//按照年龄排序的比较器
//sortAge实现Comparator接口
class sortAge implements Comparator<Student>{
@Override
//实现Comparator的compare方法
public int compare(Student stu1, Student stu2) {
// TODO Auto-generated method stub
return stu1.getAge()-stu2.getAge();
}
}
//按照名字排序的比较器
//sortAge实现Comparator接口
class sortName implements Comparator<Student>{
@Override
//实现Comparator的compare方法
public int compare(Student stu1, Student stu2) {
// TODO Auto-generated method stub
return stu1.getName().compareTo(stu2.getName());
}
}
public class SortList {
public static void main(String [] args){
//集合的定义
List<Student> list = new ArrayList<Student>();
//学生对象的加入
list.add(new Student("A",89,20));
list.add(new Student("C",60,21));
list.add(new Student("B",99,15));
//按照年龄排序
Collections.sort(list,new sortAge());
//遍历输出
System.out.println("按照年龄排序:");
for(Student stu : list){
System.out.println(stu.toString());
}
//按照名字排序
Collections.sort(list,new sortName());
//遍历输出
System.out.println("按照名字排序:");
for(Student stu : list){
System.out.println(stu.toString());
}
}
}
运行结果: