zoukankan      html  css  js  c++  java
  • 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; } }
  • 相关阅读:
    AVL树插入操作InsertAVL的实现
    epoll中EPOLLSHOT的使用
    LeetCode79:单词搜索,以及在传参时使用引用传递的重要性
    Muduo中MutexLock类中嵌套UnassignGuard类的作用
    LeetCode84:柱状图中最大的矩形
    计算图的关节点
    迪杰斯特拉算法与佛洛依德算法
    二叉树的后序遍历,先序,中序
    最近一段的学习计划
    串:KMP算法
  • 原文地址:https://www.cnblogs.com/stonesingsong/p/6547412.html
Copyright © 2011-2022 走看看