您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Java中怎么對List集合排序,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.使用 Collections 工具類中的 sort() 方法
參數不同:
void sort(List list) 在自定義類User里面實現Comparable<User>接口,并重寫抽象方法compareTo(Student o);
void sort(List list, Comparator c) 第二個參數為了省事,可以直接使用匿名內部類
public class User implements Comparable<User>{ 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();//如果年齡相等了再用分數進行排序 } return i; } } public static void main(String[] args) { List<User> users = new ArrayList<User>(); 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> students = new ArrayList<Students>(); 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<Students>() { @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()方法,傳入實現Comparator接口的實現類的實例,為了省事直接傳入匿名內部類
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> students = new ArrayList<Students>(); 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<Students>() { @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集合排序了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。