国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

list集合對sort的使用方法

這篇具有很好參考價(jià)值的文章主要介紹了list集合對sort的使用方法。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

List集合的排序:

java提供了兩種排序方式,分別是Collections.sort(List)和Collections.sort(List,Commparator),下面就這兩種方法的使用做詳細(xì)的說明:

方法一:Collections.sort(List)

這個方法有分兩種情況:1、比較的是基礎(chǔ)數(shù)據(jù) 2、比較的是引用數(shù)據(jù)

1、基礎(chǔ)數(shù)據(jù)的比較呢,一般都是直接比較,因?yàn)榛A(chǔ)數(shù)據(jù)都實(shí)現(xiàn)了CompareTo()方法,例如

 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class CommparableClass {
    /**
     * 用Collections.sort(List)排序
     * list元素的類型是String類型
     * String 類型實(shí)現(xiàn)了Commparable接口,并重寫了CompareTo方法
     * CompareTo方法中寫的是比較原則
     */
    public void sortString(){
        ArrayList<String> strs=new ArrayList<String>();
        strs.add("123");
        strs.add("987");
        strs.add("abc");
        strs.add("ABC");
 
        System.out.println("---------------原始順序,沒有排序-------------");
        for(String str:strs){
            System.out.println(str);
        }
        System.out.println("----------------------------------------");
        Collections.sort(strs);
        System.out.println("--------------- 經(jīng)過排序后輸出   --------------");
        for(String str:strs){
            System.out.println(str);
        }
    }
}
  1. 比較的是引用數(shù)據(jù),引用數(shù)據(jù)就必須要我們實(shí)現(xiàn)Comparable接口,并重寫CompareTo方法,在此方法中指定排序原則,例如

public class Student  implements Comparable<Student>{
    public String stuNo;
 
    public String name;
   
    public Integer age;
 
 
    @Override
    public int compareTo(Student o) {
        // TODO Auto-generated method stub
        //按照年齡排序
        int result1=this.getAge()-o.getAge();
        return result1;
    }
}

排序的情況如下:


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CommparableClass {
/**
     * 用Collections.sort(List)排序
     * list元素的類型是Student類型
     * String 類型實(shí)現(xiàn)了Commparable接口,并重寫了CompareTo方法
     * CompareTo方法中寫的是比較原則
     */
    public void sortStudent(){
        ArrayList<Student> stus=new ArrayList<Student>();
        Student stu1=new Student("張三",20,"S001");
        Student stu2=new Student("李四",21,"S002");
        Student stu3=new Student("王五",22,"S003");
        Student stu4=new Student("張四",22,"S004");
 
        stus.add(0,stu1); 
        stus.add(1,stu2);
        stus.add(2,stu3);
        stus.add(3,stu4);
 
        System.out.println("---------------原始順序,沒有排序-------------");
        for(Student stu:stus){
            System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
        }
        System.out.println("----------------------------------------");
        Collections.sort(stus);
        System.out.println("--------------- 經(jīng)過排序后輸出   --------------");
        for(Student str:stus){
            System.out.println("name="+str.getName()+"age="+str.getAge()+"stuNo="+str.getStuNo());
        }
    }
    }

方法二:Collections.sort(List,Commparator)

-Collections.sort(List,Commparator);

sort方法的參數(shù)有兩個,一個是要排序的List集合,另一個參數(shù)是Comparator接口,在比較器中,指定要排序的原則,

使用比較器方式就不用對要比較的集合的類類型實(shí)現(xiàn)Comparable接口

可以實(shí)現(xiàn)多個比較器,每個比較器就是一種排序原則文章來源地址http://www.zghlxwxcb.cn/news/detail-700906.html


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
 
import www.lxk.day15.demo1.Student;
 
public class ComparatorClass {
    /**
     * 此方法用于獲取一個List集合
     * @return
     */
    public List<Student> getStudents(){
        List<Student> stus=new ArrayList<Student>();
        Student stu1=new Student("張三",20,"S001");
        Student stu2=new Student("李四",21,"S002");
        Student stu3=new Student("王五",22,"S003");
        Student stu4=new Student("張四",22,"S004");
 
        stus.add(0,stu1);
        stus.add(1,stu2);
        stus.add(2,stu3);
        stus.add(3,stu4);
        return stus;
    }
    /**
     * 根據(jù)Comparator接口的子實(shí)現(xiàn)來指定排序的原則,策略模式
     * 按照姓名排序
     * @param stus
     */
    public void sortName(List<Student> stus){
        Collections.sort(stus, new Comparator<Student>(){
 
            @Override
            public int compare(Student stu1, Student stu2) {
                return stu1.getName().compareTo(stu2.getName());
            }
 
        });
        for(Student stu:stus){
            System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
        }
    }
    /**
     * 根據(jù)Comparator接口的子實(shí)現(xiàn)來指定排序的原則,策略模式
     * 按照年齡排序
     * @param stus
     */
    public void sortAge(List<Student> stus){
        Collections.sort(stus, new Comparator<Student>(){
 
            @Override
            public int compare(Student stu1, Student stu2) {
                return stu1.getAge()-stu2.getAge();
            }
 
        });
        for(Student stu:stus){
            System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
        }
    }
    /**
     * 根據(jù)Comparator接口的子實(shí)現(xiàn)來指定排序的原則,策略模式
     * 按照年齡排序
     * @param stus
     */
    public void sortstuNo(List<Student> stus){
        Collections.sort(stus, new Comparator<Student>(){
 
            @Override
            public int compare(Student stu1, Student stu2) {
                return stu1.getStuNo().compareTo(stu2.getStuNo());
            }
 
        });
        for(Student stu:stus){
            System.out.println("name="+stu.getName()+"age="+stu.getAge()+"stuNo="+stu.getStuNo());
        }
    }
}

隨機(jī)排序

Collections.shuffle(list); //進(jìn)行隨機(jī)排序

到了這里,關(guān)于list集合對sort的使用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Java02-迭代器,數(shù)據(jù)結(jié)構(gòu),List,Set ,TreeSet集合,Collections工具類

    Java02-迭代器,數(shù)據(jù)結(jié)構(gòu),List,Set ,TreeSet集合,Collections工具類

    目錄 什么是遍歷? 一、Collection集合的遍歷方式 1.迭代器遍歷 方法 流程 案例 2. foreach(增強(qiáng)for循環(huán))遍歷 案例 3.Lamdba表達(dá)式遍歷 案例 二、數(shù)據(jù)結(jié)構(gòu) 數(shù)據(jù)結(jié)構(gòu)介紹 常見數(shù)據(jù)結(jié)構(gòu) 棧(Stack) 隊(duì)列(Queue) 鏈表(Link) 散列表(Hash Table) 樹(Tree) List接口 ArraysList集合 Linked

    2024年02月14日
    瀏覽(54)
  • 使用Collections.sort方法來對自定義對象進(jìn)行排序

    Collections.sort方法可以用來對自定義對象進(jìn)行排序。要實(shí)現(xiàn)這一點(diǎn),需要在調(diào)用該方法時(shí)傳入一個比較器,該比較器用于指定如何比較對象中的值。 舉個例子,假設(shè)有一個自定義對象Person,其中包含姓名和年齡兩個屬性,我們可以使用以下代碼對該對象列表進(jìn)行按年齡排序:

    2024年02月13日
    瀏覽(25)
  • Python:pandas庫sort_values()方法的使用

    Python:pandas庫sort_values()方法的使用

    今天做到北京瑪達(dá)科技有限公司2021數(shù)據(jù)處理工程師筆試題,有一題是這樣: 按照文件中單詞出現(xiàn)頻次由高往低依次排序,這個對我來說很好實(shí)現(xiàn),用上 pandas 的 sort_values 方法就手到擒來。但是他后面又加上了一個條件,如果頻次相同的情況下,按照單詞的 MD5 值排序。這可把

    2024年02月16日
    瀏覽(22)
  • Java8-使用stream.sorted()對List排序

    1.流的定義 Stream 中文稱為 “流”,通過將集合轉(zhuǎn)換為這么一種叫做 “流” 的元素序列,通過聲明性方式,能夠?qū)现械拿總€元素進(jìn)行一系列并行或串行的操作! 如果流中的元素的類實(shí)現(xiàn)了 Comparable 接口,即有自己的排序規(guī)則,那么可以直接調(diào)用 sorted() 方法對元素進(jìn)行排

    2024年02月16日
    瀏覽(93)
  • Java 中 List 集合排序方法

    注:Collections的sort方法其實(shí)是調(diào)用了List接口自己的sort方法。 首先你需要list.parallelStream().sorted 進(jìn)行流處理,使用parallelStream可以充分調(diào)度多核CPU。 使用Comparator.comparing進(jìn)行排序,reversed()進(jìn)行倒序排列,thenComparing進(jìn)行下一個排序。 Comparator.comparing()里面的內(nèi)容,也是就是Obje

    2024年02月12日
    瀏覽(27)
  • 【Java基礎(chǔ)】Java中List集合的常用方法

    在Java編程中,List集合是最常用的一種數(shù)據(jù)結(jié)構(gòu)之一。它具有動態(tài)擴(kuò)容、元素添加、刪除和查詢等基礎(chǔ)操作,可以存儲各種類型的對象,并且支持泛型。在本文中,我將介紹Java List集合的常用方法,并通過實(shí)例演示這些方法的使用。 一、List集合的創(chuàng)建與初始化 在使用List集合

    2024年02月16日
    瀏覽(21)
  • 【Java】List集合遍歷的五種方法

    【Java】List集合遍歷的五種方法

    ??專欄【Java】 ??每日一句:人生最重要的就是要清醒的認(rèn)知 ?歡迎并且感謝大家指出我的問題 目錄 1.通過for循環(huán)配合List接口中的size()和get(index i)的方法 2.使用Iterator迭代器及其方法遍歷集合 ??迭代器 ??具體操作 3.增強(qiáng)for循環(huán)遍歷 ??是for循環(huán)的一種 ??格式 ??好處 ??弊

    2024年02月03日
    瀏覽(25)
  • 【Java】集合List的toArray()方法及其重載

    在Java中,集合(List 接口的實(shí)現(xiàn)類)提供了一個名為 toArray 的方法,用于將集合中的元素轉(zhuǎn)換成數(shù)組。該方法有兩個主要的重載形式,分別用于不同的情況。 這個方法將集合中的元素復(fù)制到一個指定類型的數(shù)組中,并返回該數(shù)組。 如果指定的數(shù)組大小足夠容納集合中的所有

    2024年02月11日
    瀏覽(26)
  • java集合之List接口實(shí)現(xiàn)類常用方法詳解

    目錄 一、List集合概述 二、ArrayList類 三、ArrayList常用方法實(shí)例 四、LinkedList類 五、Linkedist常用方法實(shí)例 ? ? ? ? java.util.List接口繼承自Collection接口,是單列集合的一個分支,通常將實(shí)現(xiàn)了List接口的對象稱為List集合,在List集合中允許出現(xiàn)重復(fù)的元素,所有的元素是以一種線

    2024年02月08日
    瀏覽(21)
  • Laravel 集合的使用 & 集合的常用方法 & 模型的數(shù)據(jù)集合 ⑩

    Laravel 集合的使用 & 集合的常用方法 & 模型的數(shù)據(jù)集合 ⑩

    @作者 : SYFStrive ? @博客首頁 : HomePage ??: THINK PHP ??: 個人社區(qū)(歡迎大佬們加入) ??: 社區(qū)鏈接?? ??: 覺得文章不錯可以點(diǎn)點(diǎn)關(guān)注 ??: 專欄連接?? ?? VUEJS (??) ?? MYSQL (??) ?? 微信小程序 (??) ?? PHPMYSQL (??) ?? UNIAPP開發(fā) (??) 提示:以下是本篇

    2024年02月10日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包