這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Java中怎么對List集合排序,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于
網(wǎng)站制作、成都做網(wǎng)站、鄲城網(wǎng)絡(luò)推廣、
成都小程序開發(fā)、鄲城網(wǎng)絡(luò)營銷、鄲城企業(yè)策劃、鄲城品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);
成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供
鄲城建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
1.使用 Collections 工具類中的 sort() 方法
參數(shù)不同:
void sort(List list) 在自定義類User里面實(shí)現(xiàn)Comparable接口,并重寫抽象方法compareTo(Student o);
void sort(List list, Comparator c) 第二個(gè)參數(shù)為了省事,可以直接使用匿名內(nèi)部類
public class User implements Comparable{ private int score; private int age; public User(int score, int age){ super(); this.score = score; this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public int compareTo(User o) { int i = this.getAge() - o.getAge();//先按照年齡排序 if(i == 0){ return this.score - o.getScore();//如果年齡相等了再用分?jǐn)?shù)進(jìn)行排序 } return i; } } public static void main(String[] args) { List users = new ArrayList(); users.add(new User(78, 26)); users.add(new User(67, 23)); users.add(new User(34, 56)); users.add(new User(55, 23)); Collections.sort(users); for(User user : users){ System.out.println(user.getScore() + "," + user.getAge()); } }
public class Students { private int age; private int score; public Students(int age, int score){ super(); this.age = age; this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } } public static void main(String[] args) { List students = new ArrayList(); students.add(new Students(23, 100)); students.add(new Students(27, 98)); students.add(new Students(29, 99)); students.add(new Students(29, 98)); students.add(new Students(22, 89)); Collections.sort(students, new Comparator() { @Override public int compare(Students o1, Students o2) { int i = o1.getScore() - o2.getScore(); if(i == 0){ return o1.getAge() - o2.getAge(); } return i; } }); for(Students stu : students){ System.out.println("score:" + stu.getScore() + ":age" + stu.getAge()); } }
2.直接使用list.sort()方法,傳入實(shí)現(xiàn)Comparator接口的實(shí)現(xiàn)類的實(shí)例,為了省事直接傳入匿名內(nèi)部類
public class Students { private int age; private int score; public Students(int age, int score){ this.age = age; this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getScore() { return score; } public void setScore(int score) { this.score = score; }}public static void main(String[] args) { List students = new ArrayList(); students.add(new Students(23, 100)); students.add(new Students(27, 98)); students.add(new Students(29, 99)); students.add(new Students(29, 98)); students.add(new Students(22, 89)); students.sort(new Comparator() { @Override public int compare(Students o1, Students o2) { int i = o1.getScore() - o2.getScore(); if (i == 0) { return o1.getAge() - o2.getAge(); } return i; } }); for (Students stu : students) { System.out.println("score:" + stu.getScore() + ":age" + stu.getAge()); }}
上述就是小編為大家分享的Java中怎么對List集合排序了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前標(biāo)題:Java中怎么對List集合排序-創(chuàng)新互聯(lián)
網(wǎng)站URL:
http://weahome.cn/article/degceo.html