• Java中Collections的sort方法和Comparable与Comparator的比较


    一、Comparable

    新建Student1类,类实现Comparable接口,并重写compareTo方法

    public class Student1 implements Comparable<Student1> {
    
        private String name;
        private int age;
    
        public Student1() {
        }
    
        public Student1(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public int compareTo(Student1 s) {
            int num = this.age - s.age;
            int num1 = (num == 0 ? this.name.compareTo(s.name) : num);
            return num1;
        }
    }

    调用

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class Student1Test {
        public static void main(String[] args) {
            List<Student1> list1 = new ArrayList<Student1>();
            list1.add(new Student1("林青霞", 27));
            list1.add(new Student1("风清扬", 30));
            list1.add(new Student1("刘晓曲", 28));
            list1.add(new Student1("武鑫", 29));
            list1.add(new Student1("林青霞", 27));
    
            Collections.sort(list1);
            for (Student1 s : list1) {
                System.out.println(s.getName() + "---" + s.getAge());
            }
        }
    }

    二、Comparator

    新建Student2类

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

    调用

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class Student2Test {
        public static void main(String[] args) {
            List<Student2> list2 = new ArrayList<Student2>();
            list2.add(new Student2("林青霞", 27));
            list2.add(new Student2("风清扬", 30));
            list2.add(new Student2("刘晓曲", 28));
            list2.add(new Student2("武鑫", 29));
            list2.add(new Student2("林青霞", 27));
    
    //方式一 Collections.sort(list2,
    new MyComparator()); for (Student2 s : list2) { System.out.println(s.getName() + "---" + s.getAge()); }
    //逆序
    //方式二:匿名类 Collections.sort(list2,
    new Comparator<Student2>() { @Override public int compare(Student2 s1, Student2 s2) { int num = s2.getAge() - s1.getAge(); int num1 = (num == 0 ? s2.getName().compareTo(s1.getName()) : num); return num1; } }); for (Student2 s : list2) { System.out.println(s.getName() + "---" + s.getAge()); } } } class MyComparator implements Comparator<Student2> { @Override public int compare(Student2 s1, Student2 s2) { int num = s1.getAge() - s2.getAge(); int num1 = (num == 0 ? s1.getName().compareTo(s2.getName()) : num); return num1; } }
  • 相关阅读:
    [Linux/wine.笔记]关于WINE(Linux上运行Windows程序的兼容层)
    [docker.笔记]常用命令
    [技巧.DotNet]超级好用的动态对象ExpandoObject
    .net core 的窗体设计器进展(.NET Core Windows Forms designer),5月中旬或将发布成熟版!
    [问题记录.Oracle/odp.net]托管ODP中,连接池的连接验证参数(validate connection=true)无效?
    [JWT]Json Web Token 备忘
    [MQ]RabbitMQ的概要介绍及消息路由规则
    常见排序算法
    C语言数值存储溢出探讨
    从计算理解数组
  • 原文地址:https://www.cnblogs.com/stonesingsong/p/6547412.html
Copyright © 2020-2023  润新知