• java对象排序


    一、前言

    有时我们需要对类按照类中的某一个属性(或者多个属性)来对类的对象进行排序,有两种方法可以实现,

    一种方法是类实现Comparable<T>接口,然后调用Collections.sort(List)方法进行排序,

    另一种方法是类不实现Comparable<T>接口,而在排序时使用Collections.sort(List, Comparator<T>)方法,并实现其中的Comparator<T>接口。

    二、排序实现

    假设有一个学生类Student,包含两个属性:姓名name,年龄age,如下:

    public class Student {
        
        private String name;
        
        private int age;
    
        ....get.set
    }

    1、通过类实现Comparable<T>接口进行排序

    public class Student implements Comparable<Student>{
    
        private String name;
    
        private int age;
    
        ...get.set
    
        /**
         * 将对象按姓名字典序升序排序
         * @param o
         * @return
         */
        @Override
        public int compareTo(Student o) {
            return this.name.compareTo(o.getName());
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    测试

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Client {
    
        public static void main(String[] args){
            List<Student> students = new ArrayList<>();
            students.add(new Student("a", 18));
            students.add(new Student("c", 19));
            students.add(new Student("b", 20));
    
            Collections.sort(students);
            for(Student student:students){
                System.out.println(student.toString());
            }
        }
    
    }

    输出

    Student{name='a', age=18}
    Student{name='b', age=20}
    Student{name='c', age=19}

    2、通过在Collections.sort()方法中实现Comparable<T>接口来实现排序

    实体类不实现Comparable接口

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Client {
    
        public static void main(String[] args){
            List<Student> students = new ArrayList<>();
            students.add(new Student("a", 18));
            students.add(new Student("c", 19));
            students.add(new Student("b", 20));
    
            Collections.sort(students, new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.getAge()>o2.getAge()? -1:(o1.getAge()==o2.getAge()? 0:1);
                }
            });
            for(Student student:students){
                System.out.println(student.toString());
            }
        }
    
    }

    结果:

    Student{name='b', age=20}
    Student{name='c', age=19}
    Student{name='a', age=18}

    可以看到学生按年龄从大到小排序输出。使用这种方法时要注意在比较函数compare的返回值中要包含0(年龄相等),不然可能会出现Comparison method violates its general contract!异常。

    三、总结

    无论使用上面的哪一种方法,对对象排序的核心是实现比较函数,其中第二种方法比第一种方法更加灵活。

    摘自:https://www.cnblogs.com/sench/p/8889435.html

  • 相关阅读:
    js的发布订阅者模式
    js打开PC的应用程序
    用node创建自己的脚手架
    用Navicat生成数据字典的方法
    Windows 11 前端基本配置【20210127】
    【win11右键】关于解决Win11环境下 vscode 重装后,不在鼠标右键的快捷栏里面
    【win11右键】关于在win11环境让鼠标右键直接显示全部功能的设置
    小程序开发 各种Demo的记录博客
    Android开发 Paging3
    adb命令 shell工具类
  • 原文地址:https://www.cnblogs.com/lyh233/p/15963361.html
Copyright © 2020-2023  润新知