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

Java使用Collections.sort()排序的方法

 更新時(shí)間:2021年12月31日 09:09:49   作者:飛渡浮舟~~  
這篇文章介紹了Java使用Collections.sort()排序的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

Java中Collections.sort()的使用

在日常開(kāi)發(fā)中,很多時(shí)候都需要對(duì)一些數(shù)據(jù)進(jìn)行排序的操作。然而那些數(shù)據(jù)一般都是放在一個(gè)集合中如:Map ,Set ,List 等集合中。他們都提共了一個(gè)排序方法 sort(),要對(duì)數(shù)據(jù)排序直接使用這個(gè)方法就行,但是要保證集合中的對(duì)象是 可比較的。
怎么讓一個(gè)對(duì)象是 可比較的,那就需要該對(duì)象實(shí)現(xiàn) Comparable<T> 接口啦。然后重寫里面的
compareTo()方法。我們可以看到Java中很多類都是實(shí)現(xiàn)類這個(gè)接口的 如:Integer,Long 等等。。。
假設(shè)我們有一個(gè)學(xué)生類,默認(rèn)需要按學(xué)生的年齡字段 age 進(jìn)行排序 代碼如下:
public class Student implements Comparable<Student> {
    private int id;
    private int age;
    private String name;

    public Student(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }
    @Override
    public int compareTo(Student o) {
        //降序
        //return o.age - this.age;
        //升序
        return this.age - o.age;        
    }
    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
這里說(shuō)一下重寫的 public int compareTo(Student o){} 這個(gè)方法,它返回三種 int 類型的值: 負(fù)整數(shù),零 ,正整數(shù)。
返回值 含義
負(fù)整數(shù) 當(dāng)前對(duì)象的值 < 比較對(duì)象的值 , 位置排在前
當(dāng)前對(duì)象的值 = 比較對(duì)象的值 , 位置不變
正整數(shù) 當(dāng)前對(duì)象的值 > 比較對(duì)象的值 , 位置排在后

測(cè)試代碼:

public static void main(String args[]){
        List<Student> list = new ArrayList<>();
        list.add(new Student(1,25,"關(guān)羽"));
        list.add(new Student(2,21,"張飛"));
        list.add(new Student(3,18,"劉備"));
        list.add(new Student(4,32,"袁紹"));
        list.add(new Student(5,36,"趙云"));
        list.add(new Student(6,16,"曹操"));
        System.out.println("排序前:");
        for (Student student : list) {
            System.out.println(student.toString());
        }
        //使用默認(rèn)排序
        Collections.sort(list);
        System.out.println("默認(rèn)排序后:");
        for (Student student : list) {
            System.out.println(student.toString());
        }
}

輸出日志:

排序前:
Student{id=1, age=25, name='關(guān)羽'}
Student{id=2, age=21, name='張飛'}
Student{id=3, age=18, name='劉備'}
Student{id=4, age=32, name='袁紹'}
Student{id=5, age=36, name='趙云'}
Student{id=6, age=16, name='曹操'}
默認(rèn)排序后:
Student{id=6, age=16, name='曹操'}
Student{id=3, age=18, name='劉備'}
Student{id=2, age=21, name='張飛'}
Student{id=1, age=25, name='關(guān)羽'}
Student{id=4, age=32, name='袁紹'}
Student{id=5, age=36, name='趙云'}

比較器的使用

這個(gè)時(shí)候需求又來(lái)了,默認(rèn)是用 age 排序,但是有的時(shí)候需要用 id 來(lái)排序怎么辦? 這個(gè)時(shí)候比較器 :Comparator 就排上用場(chǎng)了。
Comparator 的使用有兩種方式:
  • Collections.sort(list,Comparator<T>);
  • list.sort(Comparator<T>);
其實(shí)主要是看 Comparator 接口的實(shí)現(xiàn),重寫里面的 compare 方法。代碼如下:
//自定義排序1
Collections.sort(list, new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getId() - o2.getId();
    }
});

compare(Student o1, Student o2) 方法的返回值跟 Comparable<> 接口中的 compareTo(Student o) 方法 返回值意思相同。另一種寫法如下:

//自定義排序2
list.sort(new Comparator<Student>() {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getId() - o2.getId();
    }
});

輸出日志:

排序前:
Student{id=1, age=25, name='關(guān)羽'}
Student{id=2, age=21, name='張飛'}
Student{id=3, age=18, name='劉備'}
Student{id=4, age=32, name='袁紹'}
Student{id=5, age=36, name='趙云'}
Student{id=6, age=16, name='曹操'}
自定義排序后:
Student{id=1, age=25, name='關(guān)羽'}
Student{id=2, age=21, name='張飛'}
Student{id=3, age=18, name='劉備'}
Student{id=4, age=32, name='袁紹'}
Student{id=5, age=36, name='趙云'}
Student{id=6, age=16, name='曹操'}

以上所述是小編給大家介紹的Java使用Collections.sort()排序的方法,希望對(duì)大家有所幫助。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • netty中的ByteBuf源碼詳解

    netty中的ByteBuf源碼詳解

    這篇文章主要介紹了netty中的ByteBuf源碼詳解,ByteBuf,顧名思義,就是字節(jié)緩沖區(qū),是Netty中非常重要的一個(gè)組件,某些場(chǎng)景下性能不是太好,netty開(kāi)發(fā)團(tuán)隊(duì)重新設(shè)計(jì)了ByteBuf用以替代原生ByteBuffer,需要的朋友可以參考下
    2023-11-11
  • Spring中ApplicationEventPublisher發(fā)布訂閱模式的實(shí)現(xiàn)

    Spring中ApplicationEventPublisher發(fā)布訂閱模式的實(shí)現(xiàn)

    本文主要介紹了Spring中ApplicationEventPublisher發(fā)布訂閱模式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 詳解Java線程的創(chuàng)建及休眠

    詳解Java線程的創(chuàng)建及休眠

    今天帶大家學(xué)習(xí)的是Java的相關(guān)知識(shí),文章圍繞著Java線程的創(chuàng)建及休眠展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 完整B樹算法Java實(shí)現(xiàn)代碼

    完整B樹算法Java實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了完整的B樹算法Java實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • 使用google.kaptcha來(lái)生成圖片驗(yàn)證碼的實(shí)現(xiàn)方法

    使用google.kaptcha來(lái)生成圖片驗(yàn)證碼的實(shí)現(xiàn)方法

    這篇文章主要介紹了使用google.kaptcha來(lái)生成圖片驗(yàn)證碼的實(shí)現(xiàn)方法,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法

    Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法

    下面小編就為大家?guī)?lái)一篇Java 字節(jié)數(shù)組類型(byte[])與int類型互轉(zhuǎn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • 如何將mybatis配置到springmvc中

    如何將mybatis配置到springmvc中

    為了更方便的連接數(shù)據(jù)庫(kù),將mybatis配置到springMVC中。接下來(lái)通過(guò)本文給大家分享如何將mybatis配置到springmvc中,需要的朋友參考下吧
    2017-11-11
  • Java如何將大文件切割成小文件

    Java如何將大文件切割成小文件

    這篇文章主要為大家詳細(xì)介紹了Java如何將大文件切割成小文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • MyBatis insert語(yǔ)句返回主鍵和selectKey標(biāo)簽方式

    MyBatis insert語(yǔ)句返回主鍵和selectKey標(biāo)簽方式

    這篇文章主要介紹了MyBatis insert語(yǔ)句返回主鍵和selectKey標(biāo)簽方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼

    Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼

    這篇文章主要介紹了Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10

最新評(píng)論