美文网首页Java 杂谈Java
javaSe-Comparable和Comparator

javaSe-Comparable和Comparator

作者: 叫我懒猫 | 来源:发表于2019-06-12 15:44 被阅读1次
    两者区别

    Comparable用法

    ! 这玩意只能进行自然升序排序,不可自定义排序规则。

    class Human implements Comparable<Human>{
        private String name;
        private int age;
        public Human(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 int compareTo(Human o) {
            if(this.age == o.age && this.name == o.name) {
                return 0;
            }else if(this.age > o.age) {
                return 1;
            }else {
                return -1;
            }
        }
        @Override
        public String toString() {
            return "Human [name=" + name + ", age=" + age + "]";
        }
    }
    public class TestLambda {
        public static void main(String[] args) {
            List<Human> humans = new ArrayList<>(); 
            humans.add(new Human("A",55));
            humans.add(new Human("Bbbbbbbb",9));
            humans.add(new Human("cC",15));
            Collections.sort(humans);
            humans.forEach(System.out::println);    
        }
    }
    

    Comparator

    ! 在compare(Object o1,Object o2)里呢,相比上边的那个comparable,这里的comparator可以进行自定义的排序,比如按照升序排序:

    • o1 == o2 就返回 0
    • o1 > o2 就返回 1
    • o1 < o2 就返回 -1

    ! 当然按照降序的话,就把1和-1换个位置。

    class Human{
        public int age;
        public 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 Human(int age,String name) {
            super();
            this.age = age;
            this.name = name;
        }
        @Override
        public String toString() {
            return "Human [age=" + age + ", name=" + name + "]";
        }
    }
    public class TestLambda {
        public static void main(String[] args) {
            List<Human> list = new ArrayList<Human>();
            list.add(new Human(111,"aaa"));
            list.add(new Human(22,"b"));
            list.add(new Human(55,"hhH"));
            Collections.sort(list,new Comparator<Human>() {
                @Override
                public int compare(Human o1, Human o2) {
                    if(o1.age == o2.age && o1.name == o2.name) {
                        return 0;
                    }else if(o1.age > o2.age) {
                        return -1;
                    }else {
                        return 1;
                    }
                }
            });
            list.forEach(System.out::println);
        }
    }
    

    相关文章

      网友评论

        本文标题:javaSe-Comparable和Comparator

        本文链接:https://www.haomeiwen.com/subject/djrlfctx.html