zoukankan      html  css  js  c++  java
  • 使用Comparable和Comparator排序

    一、排序规则

    Comparable和Comparator都是函数式接口,两者的使用大同小异。

    比较规则:

    1、Comparable下有个comparaTo(T o)方法,调用 a.comparaTo(b),如果是正数,则a比b大;如果是负数,则a比b小;如果是0,则相等。

    2、Comparator下有个compara(T o1,To2)方法,调用compara(T o1,To2),如果是正数,则a比b大;如果是负数,则a比b小;如果是0,则相等。

    同时,按照该排序逻辑,返回的数据默认有小到大排列。

    使用方式:

    1、Comparable被实体类继承,然后重写comparaTo方法。实体类实现Comparable,重写comparaTo方法。就可以被一些集合方法调用,实现排序。如TreeSet和TreeMap的自动排序(其实,前者调用后者)和Collections.sort()和Arrays.sort()。

    1、Comparator一般被当做参数传递给集合类,如List的sort方法。

    二、实现方式

    1、Comparable的排序

    实体类:

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Student implements Comparable<Student>{
        private String name;
        private Integer age;
    
        public int compareTo(Student o) {
            //先按name排序
            if(this.name.compareTo(o.getName())>0)
                return 1;
            if(this.name.compareTo(o.getName())<0)
                return -1;
    
            //再按age排序
            if(this.age > o.age)
                return 1;
            if(this.age < o.getAge())
                return -1;
            return 0;
        }
    }

    测试代码:

    import org.junit.Test;
    
    import java.util.*;
    
    public class AppTest {
    
        @Test
        public void test(){
            ArrayList<Student> list = new ArrayList();
            list.add(new Student("tom",22));
            list.add(new Student("tom",24));
            list.add(new Student("tom",2));
            System.out.println(list);
            Collections.sort(list);
            System.out.println(list);
    
        }
    }

    结果:

    [Student(name=tom, age=22), Student(name=tom, age=24), Student(name=tom, age=2)]
    [Student(name=tom, age=2), Student(name=tom, age=22), Student(name=tom, age=24)]

    2、Comparator的排序

    实体类:

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Student{
        private String name;
        private Integer age;
    }

    测试代码:

    import org.junit.Test;
    
    import java.util.*;
    
    public class AppTest {
    
        @Test
        public void test(){
            ArrayList<Student> list = new ArrayList();
            list.add(new Student("tom",22));
            list.add(new Student("tom",24));
            list.add(new Student("tom",2));
            System.out.println(list);
            list.sort((c1,c2) -> c1.getAge()-c2.getAge());//lambda表达式
            System.out.println(list);
    
        }
    }

    结果:

    [Student(name=tom, age=22), Student(name=tom, age=24), Student(name=tom, age=2)]
    [Student(name=tom, age=2), Student(name=tom, age=22), Student(name=tom, age=24)]

    注意:两个比较对象的顺序写错会直接导致排序方式变化,由正序变成反序。

    就算这个世道烂成一堆粪坑,那也不是你吃屎的理由
  • 相关阅读:
    Response.Redirect 打开新窗体的两种方法
    linux下coredump的产生及调试方法
    AlertDialog具体解释
    数据仓库与数据挖掘的一些基本概念
    JS中setTimeout()的使用方法具体解释
    iOS开发- 查询项目代码行数
    STM32学习之路-LCD(3)&lt;显示图片&gt;
    谷歌技术&quot;三宝&quot;之MapReduce
    [ffmpeg 扩展第三方库编译系列] 关于libvpx mingw32编译问题
    javascript笔记
  • 原文地址:https://www.cnblogs.com/whalesea/p/12934786.html
Copyright © 2011-2022 走看看