最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java對(duì)ArrayList中元素進(jìn)行排序的幾種方式總結(jié)

 更新時(shí)間:2024年08月19日 08:57:20   作者:Ez4Sterben  
在Java中,ArrayList類提供了多種排序方法,可以根據(jù)不同的需求選擇適合的排序方法,下面這篇文章主要給大家介紹了關(guān)于java對(duì)ArrayList中元素進(jìn)行排序的幾種方式,需要的朋友可以參考下

一、使用Collections工具類

1、對(duì)基本類型排序

通過(guò)Collections.sort()對(duì)基本類型排序默認(rèn)是以升序排序

// 1.Collections.sort()默認(rèn)按照升序排序
List<Integer> integerList = new ArrayList<>();
Collections.addAll(integerList,1,2,6,5,5,4,55,4,5,5,4,5,2,4,6,2,45);
Collections.sort(integerList);
System.out.println(integerList);

2、對(duì)字符串類型排序

對(duì)字符串類型排序默認(rèn)按照首字母a-z排序

// 2.對(duì)字符串類型排序
List<String> strings = new ArrayList<>();
Collections.addAll(strings,"d","gsf","trh","fsd","an");
Collections.sort(strings);
System.out.println(strings);

3、對(duì)對(duì)象排序

如何使用Collections對(duì)對(duì)象排序呢?

其實(shí)只需要讓我們的數(shù)據(jù)類型實(shí)現(xiàn)Comparable接口即可,下面定義一個(gè)實(shí)現(xiàn)Comparable接口的學(xué)生類,并且實(shí)現(xiàn)compareTo方法,讓學(xué)生類只比較年齡。

/**
 * 學(xué)生
 *
 * @author ez4sterben
 * @date 2023/07/18
 */
public class Student implements Comparable<Student> {

    private String id;
    private String name;
    private Integer age;
    private String sex;

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                '}';
    }

    public Student(Integer age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public int compareTo(Student o) {
    	// 這種是升序
        return this.getAge() - o.getAge();
        // 這種是降序
        // return o.getAge() - this.getAge();
    }
}

排序方法和正常使用一樣,直接把整個(gè)list傳入即可

// 3.對(duì)對(duì)象排序
        List<Student> students = new ArrayList<>();
        Collections.addAll(students,
                new Student(18),
                new Student(26),
                new Student(20),
                new Student(16),
                new Student(12));
        System.out.println(students);
        Collections.sort(students);
        System.out.println(students);

二、使用stream流

// 2.lambda表達(dá)式
        Stream<Integer> sorted = integerList.stream().sorted();
        System.out.println(Arrays.toString(sorted.toArray()));

三、使用排序算法(以冒泡排序?yàn)槔?/h2>
// 3.直接使用排序算法(以冒泡排序?yàn)槔?
        for(int i = 0; i < integerList.size() - 1; i++){
            for(int j = 0; j < integerList.size() - i - 1; j++){
                if(integerList.get(j) > integerList.get(j + 1)){
                    Integer temp = integerList.get(j);
                    integerList.set(j, integerList.get(j + 1));
                    integerList.set(j + 1, temp);
                }
            }
        }
        System.out.println(integerList);

四、測(cè)試類整體代碼

import java.util.*;
import java.util.stream.Stream;

/**
 * 數(shù)組列表排序
 *
 * @author ez4sterben
 * @date 2023/07/19
 */
public class ArrayListSort {
    public static void main(String[] args) {
        List<Integer> integerList = new ArrayList<>();
        Collections.addAll(integerList,1,2,6,5,5,4,55,4,5,5,4,5,2,4,6,2,45);
        // 1.Collections.sort()默認(rèn)按照升序排序
        Collections.sort(integerList);
        System.out.println(integerList);
        // 2.對(duì)字符串類型排序
        List<String> strings = new ArrayList<>();
        Collections.addAll(strings,"d","gsf","trh","fsd","an");
        Collections.sort(strings);
        System.out.println(strings);
        // 3.對(duì)對(duì)象排序
        List<Student> students = new ArrayList<>();
        Collections.addAll(students,
                new Student(18),
                new Student(26),
                new Student(20),
                new Student(16),
                new Student(12));
        System.out.println(students);
        Collections.sort(students);
        System.out.println(students);

        // 2.lambda表達(dá)式
        Stream<Integer> sorted = integerList.stream().sorted();
        System.out.println(Arrays.toString(sorted.toArray()));

        // 3.直接使用排序算法(以冒泡排序?yàn)槔?
        for(int i = 0; i < integerList.size() - 1; i++){
            for(int j = 0; j < integerList.size() - i - 1; j++){
                if(integerList.get(j) > integerList.get(j + 1)){
                    Integer temp = integerList.get(j);
                    integerList.set(j, integerList.get(j + 1));
                    integerList.set(j + 1, temp);
                }
            }
        }
        System.out.println(integerList);

    }
}

總結(jié) 

到此這篇關(guān)于java對(duì)ArrayList中元素進(jìn)行排序的幾種方式總結(jié)的文章就介紹到這了,更多相關(guān)java對(duì)ArrayList元素排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一文詳解Spring?security框架的使用

    一文詳解Spring?security框架的使用

    Spring?Security是一個(gè)基于Spring框架的安全認(rèn)證和授權(quán)框架,它提供了一套全面的安全解決方案,可以在Web應(yīng)用、移動(dòng)應(yīng)用和Web服務(wù)等不同場(chǎng)景下使用。本文就來(lái)詳細(xì)聊聊它的使用吧
    2023-03-03
  • Spring中@Autowired、@Qualifier、@Resource注解的區(qū)別

    Spring中@Autowired、@Qualifier、@Resource注解的區(qū)別

    這篇文章主要介紹了Spring中@Autowired、@Qualifier、@Resource注解的區(qū)別,@Autowired 可以單獨(dú)使用,如果單獨(dú)使用,它將按類型裝配,因此,如果在容器中聲明了多個(gè)相同類型的bean,則會(huì)出現(xiàn)問(wèn)題,因?yàn)?nbsp;@Autowired 不知道要使用哪個(gè)bean來(lái)注入,需要的朋友可以參考下
    2023-11-11
  • Java微信公眾號(hào)推送模版消息的步驟示例詳解

    Java微信公眾號(hào)推送模版消息的步驟示例詳解

    模板消息是一種向用戶發(fā)送通知的服務(wù),廣泛用于訂單狀態(tài)更新、服務(wù)提醒等場(chǎng)景,下面,我將詳細(xì)介紹如何使用Java結(jié)合微信官方提供的API來(lái)實(shí)現(xiàn)模板消息的推送,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • Java21增強(qiáng)對(duì)Emoji表情符號(hào)處理示例詳解

    Java21增強(qiáng)對(duì)Emoji表情符號(hào)處理示例詳解

    這篇文章主要為大家介紹了Java21增強(qiáng)對(duì)Emoji表情符號(hào)處理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • 解決nacos修改配置信息后需要重啟服務(wù)才能生效的問(wèn)題

    解決nacos修改配置信息后需要重啟服務(wù)才能生效的問(wèn)題

    當(dāng)配置信息發(fā)生變動(dòng)時(shí),傳統(tǒng)修改配置信息后,需要重新重啟服務(wù)器才可以生效,大量應(yīng)用配置修改時(shí),需要一個(gè)個(gè)修改配置,無(wú)法統(tǒng)一修改,且沒(méi)有辦法回溯配置版本,所以本文給大家介紹了如何解決這些問(wèn)題的方法,需要的朋友可以參考下
    2023-10-10
  • SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問(wèn)應(yīng)用的實(shí)例代碼

    SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問(wèn)應(yīng)用的實(shí)例代碼

    這篇文章主要介紹了SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問(wèn)應(yīng)用的實(shí)例代碼,需要的朋友可以參考下
    2017-05-05
  • Sprigmvc項(xiàng)目轉(zhuǎn)為springboot的方法

    Sprigmvc項(xiàng)目轉(zhuǎn)為springboot的方法

    本篇文章主要介紹了Sprigmvc項(xiàng)目轉(zhuǎn)為springboot的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • java實(shí)現(xiàn)字符串like和not?like的使用示例

    java實(shí)現(xiàn)字符串like和not?like的使用示例

    在Java中,我們經(jīng)常需要對(duì)字符串進(jìn)行模式匹配操作,字符串的模式匹配通常使用like和not?like這兩個(gè)運(yùn)算符進(jìn)行,本文就來(lái)介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下
    2023-09-09
  • Spring?Cloud?Gateway編碼實(shí)現(xiàn)任意地址跳轉(zhuǎn)的示例

    Spring?Cloud?Gateway編碼實(shí)現(xiàn)任意地址跳轉(zhuǎn)的示例

    本文主要介紹了Spring?Cloud?Gateway編碼實(shí)現(xiàn)任意地址跳轉(zhuǎn)的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • idea導(dǎo)入springboot項(xiàng)目沒(méi)有maven的解決

    idea導(dǎo)入springboot項(xiàng)目沒(méi)有maven的解決

    這篇文章主要介紹了idea導(dǎo)入springboot項(xiàng)目沒(méi)有maven的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評(píng)論

德惠市| 萝北县| 恩平市| 萨迦县| 灯塔市| 内乡县| 峨眉山市| 和平县| 天水市| 肇东市| 天长市| 内江市| 琼结县| 福贡县| 会泽县| 清新县| 昌都县| 兰坪| 洪雅县| 清新县| 滨州市| 棋牌| 梅州市| 文化| 阜南县| 静海县| 峨眉山市| 玛曲县| 罗江县| 新津县| 雅江县| 伊川县| 贵溪市| 南城县| 巴林右旗| 屏东市| 朔州市| 邹平县| 阿城市| 永德县| 密山市|