• 【List、Set、数据结构、Collections】-Collections


    day03 【List、Set、数据结构、Collections】

    5.3 简述Comparable和Comparator两个接口的区别。

    Comparable:强行对实现它的每个类的对象进行整体排序。这种排序被称为类的自然排序,类的compareTo方法被称为它的自然比较方法。只能在类中实现compareTo()一次,不能经常修改类的代码实现自己想要的排序。实现此接口的对象列表(和数组)可以通过Collections.sort(和Arrays.sort)进行自动排序,对象可以用作有序映射中的键或有序集合中的元素,无需指定比较器。

    Comparator强行对某个对象进行整体排序。可以将Comparator 传递给sort方法(如Collections.sort或 Arrays.sort),从而允许在排序顺序上实现精确控制。还可以使用Comparator来控制某些数据结构(如有序set或有序映射)的顺序,或者为那些没有自然顺序的对象collection提供排序。

    5.4 练习

    创建一个学生类,存储到ArrayList集合中完成指定排序操作。

    Student 初始类

    public class Student{
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        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;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                   "name='" + name + '\'' +
                   ", age=" + age +
                   '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    测试类:

    public class Demo {
    
        public static void main(String[] args) {
            // 创建四个学生对象 存储到集合中
            ArrayList<Student> list = new ArrayList<Student>();
    
            list.add(new Student("rose",18));
            list.add(new Student("jack",16));
            list.add(new Student("abc",16));
            list.add(new Student("ace",17));
            list.add(new Student("mark",16));
    
    
            /*
              让学生 按照年龄排序 升序
             */
    //        Collections.sort(list);//要求 该list中元素类型  必须实现比较器Comparable接口
    
    
            for (Student student : list) {
                System.out.println(student);
            }
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    发现,当我们调用Collections.sort()方法的时候 程序报错了。

    原因:如果想要集合中的元素完成排序,那么必须要实现比较器Comparable接口。

    于是我们就完成了Student类的一个实现,如下:

    public class Student implements Comparable<Student>{
        ....
        @Override
        public int compareTo(Student o) {
            return this.age-o.age;//升序
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    再次测试,代码就OK 了效果如下:

    Student{name='jack', age=16}
    Student{name='abc', age=16}
    Student{name='mark', age=16}
    Student{name='ace', age=17}
    Student{name='rose', age=18}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5.5 扩展

    如果在使用的时候,想要独立的定义规则去使用 可以采用Collections.sort(List list,Comparetor c)方式,自己定义规则:

    Collections.sort(list, new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            return o2.getAge()-o1.getAge();//以学生的年龄降序
        }
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    效果:

    Student{name='rose', age=18}
    Student{name='ace', age=17}
    Student{name='jack', age=16}
    Student{name='abc', age=16}
    Student{name='mark', age=16}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    如果想要规则更多一些,可以参考下面代码:

    Collections.sort(list, new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    // 年龄降序
                    int result = o2.getAge()-o1.getAge();//年龄降序
    
                    if(result==0){//第一个规则判断完了 下一个规则 姓名的首字母 升序
                        result = o1.getName().charAt(0)-o2.getName().charAt(0);
                    }
    
                    return result;
                }
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    效果如下:

    Student{name='rose', age=18}
    Student{name='ace', age=17}
    Student{name='abc', age=16}
    Student{name='jack', age=16}
    Student{name='mark', age=16}
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    Qt鼠标事件全面解析:从基础到实战
    Libtorch各版本下载
    EasyRecovery易恢复2023最新版数据恢复软件功能特色介绍
    软考 系统架构设计师 简明教程 | 软件开发方法
    DBeaverUE Mac版:数据库管理新纪元,一键掌控所有数据
    AIGC: 关于ChatGPT的提问方式和Prompt工程
    04.爱芳地产项目小程序全栈项目经验(已上线)
    ES6 从入门到精通 # 20:async 的用法
    【开源】基于Vue.js的音乐偏好度推荐系统的设计和实现
    微信小程序自定义tabBar(实操)
  • 原文地址:https://blog.csdn.net/qq_28770419/article/details/136753904