java 集合中自定义对象的几种排序方法

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());
        }
    }
}

运行结果:运行结果

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值