欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中Comparable與Comparator的區(qū)別解析

 更新時間:2024年01月11日 08:37:06   作者:苦糖果與忍冬  
這篇文章主要介紹了Java中Comparable與Comparator的區(qū)別解析,實現(xiàn)Comparable接口,重寫compareTo方法,一般在實體類定義的時候就可以選擇實現(xiàn)該接口,提供一個默認的排序方式,供Arrays.sort和Collections.sort使用,需要的朋友可以參考下

Comparable與Comparator的區(qū)別

1.理論知識

Comparable

實現(xiàn)Comparable接口,重寫compareTo方法。一般在實體類定義的時候就可以選擇實現(xiàn)該接口,提供一個默認的排序方式,供Arrays.sort和Collections.sort使用,只有一種排序方式,很難滿足復雜的排序要求。

compareTo方法的返回值是int,有三種情況:

a、當前對象大于被比較對象(compareTo方法里面的形參),返回正整數(shù)

b、當前對象等于被比較對象,返回0

c、當前對象小于被比較對象,返回負整數(shù)

Comparator

實現(xiàn)Comparator接口,重寫compare方法,一般在實體類中未定義排序方法或?qū)嶓w類中的排序方法不滿足需求的情況下來實現(xiàn)該接口。實現(xiàn)方法可以是寫一個比較器類,也可是匿名內(nèi)部類。實現(xiàn)方式靈活,可以提供多種排序選擇。

Ccompare方法有兩個參數(shù)o1和o2,分別表示待比較的兩個對象,方法返回值也是int,有三種情況:

a、o1大于o2,返回正整數(shù)

b、o1等于o2,返回0

c、o1小于o2,返回負整數(shù)

2.代碼驗證

上代碼

實體類Student,實現(xiàn)Comparable接口

public class Student implements Comparable<Student> {
    private int age;
    private String address;
    private int score;
    public Student(int age, String address, int score) {
        this.age = age;
        this.address = address;
        this.score = score;
    }
    public int getAge() {
        return age;
    }
    public String getAddress() {
        return address;
    }
    public int getScore() {
        return score;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public void setScore(int score) {
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", address='" + address + '\'' +
                ", score=" + score +
                '}';
    }
    @Override
    public int compareTo(Student o) {
//        return this.age - o.age;
        return o.age - this.age;
    }
}

測試類

public class SortTest {
    public static void main(String[] args) {
        Student s1 = new Student(18, "西安", 98);
        Student s2 = new Student(6, "廣州", 68);
        Student s3 = new Student(29, "天水", 84);
        Student s4 = new Student(18, "廣州", 84);
        Student[] students = new Student[4];
        students[0] = s1;
        students[1] = s2;
        students[2] = s3;
        students[3] = s4;
        Arrays.sort(students);
//        Arrays.sort(students,new StudentScoreComparator());
//        Arrays.sort(students, new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                return o1.getScore() - o2.getScore();
//            }
//        });
//        Arrays.sort(students, (o1, o2) -> o1.getScore() - o2.getScore());
//        Arrays.sort(students, Comparator.comparingInt(Student::getScore));
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i]);
        }
    }
}

比較器類

public class StudentScoreComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        if (o1.getScore() == o2.getScore()) {
            return o1.getAge() - o2.getAge();
        }
        return o2.getScore() - o1.getScore();
    }
}
  • 使用默認排序(前提是實體類實現(xiàn)Comparable接口,否則會報錯)
Arrays.sort(students);

compareTo方法:用age字段排序

正序:this.age - o.age;

逆序:o.age - this.age;

  • 使用比較器類排序
Arrays.sort(students,new StudentScoreComparator());

compare方法:score逆序,age正序

正序:o1-o2

逆序:o2-o1

匿名內(nèi)部類排序:score正序

Arrays.sort(students, new Comparator() {
@Override
public int compare(Student o1, Student o2) {
    return o1.getScore() - o2.getScore();
    }
});

lambda簡化:

Arrays.sort(students, (o1, o2) -> o1.getScore() - o2.getScore());

正序可以再簡化,逆序不行

Arrays.sort(students, Comparator.comparingInt(Student::getScore));

到此這篇關(guān)于Java中Comparable與Comparator的區(qū)別解析的文章就介紹到這了,更多相關(guān)Comparable與Comparator的區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一篇文章帶你了解Java中ThreadPool線程池

    一篇文章帶你了解Java中ThreadPool線程池

    線程池可以控制運行的線程數(shù)量,本文就線程池做了詳細的介紹,需要了解的小伙伴可以參考一下
    2021-08-08
  • MyBatis通用Mapper實現(xiàn)原理及相關(guān)內(nèi)容

    MyBatis通用Mapper實現(xiàn)原理及相關(guān)內(nèi)容

    今天小編就為大家分享一篇關(guān)于MyBatis通用Mapper實現(xiàn)原理及相關(guān)內(nèi)容,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java中類的定義和初始化示例詳解

    Java中類的定義和初始化示例詳解

    這篇文章主要給大家介紹了關(guān)于Java中類的定義和初始化的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • 50 道Java 線程面試題(經(jīng)典)

    50 道Java 線程面試題(經(jīng)典)

    java 線程面試題是比較熱門的面試題,下面小編給大家分享了50道java線程面試題必掌握,大家來一起學習吧為面試好好準備吧
    2016-11-11
  • Servlet方法生命周期及執(zhí)行原理詳解

    Servlet方法生命周期及執(zhí)行原理詳解

    運行在服務(wù)器端的小程序,Servlet就是一個接口,定義了Java類被瀏覽器訪問到(tomcat識別)的規(guī)則,將來我們自定義一個類,實現(xiàn)Servlet接口,復寫方法
    2021-09-09
  • SpringBoot自定義bean綁定實現(xiàn)

    SpringBoot自定義bean綁定實現(xiàn)

    這篇文章主要介紹了SpringBoot自定義bean綁定,最常見的配置綁定的場景,是在自定義的bean中通過@Value注解將某個屬性和對應的配置綁定
    2022-10-10
  • 使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應用

    使用Spring Boot快速構(gòu)建基于SQLite數(shù)據(jù)源的應用

    為了提供一個單包易部署的服務(wù)器應用,考慮使用Spring Boot,因為其集成了Apache Tomcat,易于運行,免去絕大部分了服務(wù)器配置的步驟
    2017-08-08
  • SpringMVC異常處理知識點總結(jié)

    SpringMVC異常處理知識點總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于SpringMVC異常處理相關(guān)知識點內(nèi)容,需要的朋友們學習下。
    2019-10-10
  • Java8中Optional類型和Kotlin中可空類型的使用對比

    Java8中Optional類型和Kotlin中可空類型的使用對比

    這篇文章主要給大家介紹了關(guān)于Java8中Optional類型和Kotlin中可空類型的使用對比,文中通過示例代碼給大家介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-09-09
  • SpringBoot?內(nèi)置工具類的使用

    SpringBoot?內(nèi)置工具類的使用

    本文主要介紹了SpringBoot?內(nèi)置工具類的使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12

最新評論