Java对List集合中的元素排序

JDK1.8之前这样做;

//创建bean

public class Student {
    
    private int age;
    private String name;
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Student(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    
    public String toString() {
        return "Student [age=" + age + ", name=" + name + "]";
    }
    
}

//测试

public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(40, "张三"));
        list.add(new Student(18, "小强"));
        list.add(new Student(30, "王强"));

        Collections.sort(list, new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                // 按照年龄进行升序排列
                if (s1.getAge() > s2.getAge()) {
                    return 1;
                }
                if (s1.getAge() == s2.getAge()) {
                    return 0;
                }
                return -1;
            }
        });
        System.out.println("排序后的结果:" + list);

}

排序后的结果:[Student [age=18, name=小强], Student [age=30, name=王强], Student [age=40, name=张三]] 

也可以这样; 

//创建bean

public class Student implements Comparable{
    
    private int age;
    private String name;
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Student(int age, String name) {
        super();
        this.age = age;
        this.name = name;
    }
    
    public int compareTo(Object o) {
        Student student = (Student)o;
        if(this.age > student.age){
            return 1;
        } 
        if(this.age < student.age){
            return -1;
        }
        return 0;
    }

    
    public String toString() {
        return "Student [age=" + age + ", name=" + name + "]";
    }
    
}

//创建测试类

public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(40, "张三"));
        list.add(new Student(18, "小强"));
        list.add(new Student(30, "王强"));
 
        System.out.println("排序前的结果:" + list);
        Collections.sort(list); 
        System.out.println("排序后的结果:" + list);
    }

排序前的结果:[Student [age=40, name=张三], Student [age=18, name=小强], Student [age=30, name=王强]]
排序后的结果:[Student [age=18, name=小强], Student [age=30, name=王强], Student [age=40, name=张三]] 

JDK1.8之后这样做;

public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(40, "张三"));
        list.add(new Student(18, "小强"));
        list.add(new Student(30, "王强"));

        list.sort(new Comparator<Student>() {
            public int compare(Student s1, Student s2) {
                // 按照年龄进行升序排列
                if (s1.getAge() > s2.getAge()) {
                    return 1;
                }
                if (s1.getAge() == s2.getAge()) {
                    return 0;
                }
                return -1;
            }
        });
        System.out.println("排序后的结果:" + list);
    }

排序后的结果:[Student [age=18, name=小强], Student [age=30, name=王强], Student [age=40, name=张三]]
还可以这样做; 

public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student(40, "张三"));
        list.add(new Student(18, "小强"));
        list.add(new Student(30, "王强"));
        
        list.sort(Comparator.comparing(Student::getAge));
        System.out.println("升序后的结果:" + list);
        list.sort(Comparator.comparing(Student::getAge).reversed());
        System.out.println("降序后的结果:" + list);
    }

升序后的结果:[Student [age=18, name=小强], Student [age=30, name=王强], Student [age=40, name=张三]]
降序后的结果:[Student [age=40, name=张三], Student [age=30, name=王强], Student [age=18, name=小强]]
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值