Android中list集合的排序方法
阿新 • • 發佈:2019-01-31
Collections對List集合中的資料進行排序
有時候需要對集合中的元素按照一定的規則進行排序,這就需要用到
[3, 5, 1, 0]
[0, 1, 3, 5]
55,23
67,23
78,26
34,56
我們會發現sort(List<T>)方法中List中的T必須實現Comparable<T>介面,然後實現
compareTo()方法,該方法的返回值0代表相等,1表示大於,-1表示小於;為什麼
在簡單例子中沒有看到實現Comparable介面呢?是因為Integer類其實自己已經實現
了Comparable介面,Java已經給我們做好了。
Collections提供的第二種排序方法sort(List<T> list, Comparator<? super T> c)
先看例子:
[java] view plain copy print?
score:89:age22
score:98:age27
score:98:age29
score:99:age29
score:100:age23
從上面的例子我們可以看出Students類沒有實現Comparable<T>介面,只是在sort()方法
中多傳入一個引數,只不過該引數是一個介面我們需要實現其compare方法。
以上就是是Java中Colelctions工具類為我們提供的兩種集合排序方法。
有時候需要對集合中的元素按照一定的規則進行排序,這就需要用到
Java中提供的對集合進行操作的工具類Collections,其中的sort方法
先看一個簡單的例子:
[java] view plain copy print?- publicstaticvoid main(String[] args) {
- List<Integer> nums = new ArrayList<Integer>();
- nums.add(3);
- nums.add(5);
- nums.add(1
- nums.add(0);
- System.out.println(nums);
- Collections.sort(nums);
- System.out.println(nums);
- }
輸出結果:public static void main(String[] args) { List<Integer> nums = new ArrayList<Integer>(); nums.add(3); nums.add(5); nums.add(1); nums.add(0); System.out.println(nums); Collections.sort(nums); System.out.println(nums); }
[3, 5, 1, 0]
[0, 1, 3, 5]
稍微複雜的List裡面放一個複雜的物件
[java] view plain copy print?- package core.java.collection.collections;
- publicclass User implements Comparable<User>{
- privateint score;
- privateint age;
- public User(int score, int age){
- super();
- this.score = score;
- this.age = age;
- }
- publicint getScore() {
- return score;
- }
- publicvoid setScore(int score) {
- this.score = score;
- }
- publicint getAge() {
- return age;
- }
- publicvoid setAge(int age) {
- this.age = age;
- }
- @Override
- publicint compareTo(User o) {
- int i = this.getAge() - o.getAge();//先按照年齡排序
- if(i == 0){
- returnthis.score - o.getScore();//如果年齡相等了再用分數進行排序
- }
- return i;
- }
- }
- publicstaticvoid 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());
- }
- }
package core.java.collection.collections;
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());
}
}
輸出結果:55,23
67,23
78,26
34,56
我們會發現sort(List<T>)方法中List中的T必須實現Comparable<T>介面,然後實現
compareTo()方法,該方法的返回值0代表相等,1表示大於,-1表示小於;為什麼
在簡單例子中沒有看到實現Comparable介面呢?是因為Integer類其實自己已經實現
了Comparable介面,Java已經給我們做好了。
Collections提供的第二種排序方法sort(List<T> list, Comparator<? super T> c)
先看例子:
[java] view plain copy print?
- package core.java.collection.collections;
- publicclass Students {
- privateint age;
- privateint score;
- public Students(int age, int score){
- super();
- this.age = age;
- this.score = score;
- }
- publicint getAge() {
- return age;
- }
- publicvoid setAge(int age) {
- this.age = age;
- }
- publicint getScore() {
- return score;
- }
- publicvoid setScore(int score) {
- this.score = score;
- }
- }
- publicstaticvoid 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
- publicint 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());
- }
- }
package core.java.collection.collections;
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());
}
}
輸出結果:score:89:age22
score:98:age27
score:98:age29
score:99:age29
score:100:age23
從上面的例子我們可以看出Students類沒有實現Comparable<T>介面,只是在sort()方法
中多傳入一個引數,只不過該引數是一個介面我們需要實現其compare方法。
以上就是是Java中Colelctions工具類為我們提供的兩種集合排序方法。