大半夜,一同学来问这段代码.问了三个问题.
一,这个排序Sort()怎么用?
二,接口作为方法的参数是什么意思?
三,入参直接new Comparator(){}是怎么回事?
先回答第二,三个问题:
Collections.sort(list, new Comparator<User>() {
public int compare(User o1, User o2) {
//o1-o2降序(顺序),反之o2-o1则升序(反序).
int ageC = o1.getAge() - o2.getAge();
if (ageC == 0) {
return o1.getName().compareTo(o2.getName());
} else {
return ageC;
} }
});
//去掉User也可以 Collections.sort(list, (o1, o2) ->{
Collections.sort(list, (User o1, User o2) -> {
//o1-o2降序(顺序),反之o2-o1则升序(反序).
int ageC = o1.getAge() - o2.getAge();
if (ageC == 0) {
return o1.getName().compareTo(o2.getName());
} else {
return ageC;
}
});
最后说下Collections.sort()两种用法.这是JDK java.util包自带的排序方法.这两个方法都是调用Arrays.sort(), 最终排序算法是legacyMergeSort()_归并排序或 TimSort.sort()_结合了合并排序(merge sort)和插入排序(insertion sort)而得出的排序算法
//第一种方法
public static <T> void sort(List<T> list, Comparator super T> c) {
list.sort(c);
}
//第二种方法
public static <T extends Comparable super T>> void sort(List<T> list) {
list.sort(null);
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestSort {
public static void main(String[] args) {
List<User> list = new ArrayList();
list.add(new User(10, "test1"));
list.add(new User(11, "test2"));
list.add(new User(8, "test3"));
//第一种用法1
Collections.sort(list, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
//o1-o2降序(顺序),反之o2-o1则升序(反序).
int ageC = o1.getAge() - o2.getAge();
if (ageC == 0) {
//String类重写了compareTo方法
return o1.getName().compareTo(o2.getName());
} else {
return ageC;
}
}
});
System.out.println(list.toString());
//第一种用法2
//jdk1.8lambda表达式写法
Collections.sort(list, (User o1, User o2) -> {
//o1-o2降序(顺序),反之o2-o1则升序(反序).
int ageC = o1.getAge() - o2.getAge();
if (ageC == 0) {
return o1.getName().compareTo(o2.getName());
} else {
return ageC;
}
});
List<UserC> list2 = new ArrayList();
list2.add(new UserC(10, "test1"));
list2.add(new UserC(11, "test2"));
list2.add(new UserC(9, "test3"));
//第二种用法
Collections.sort(list2);
System.out.println(list2.toString());
}
//静态内部类,方便写demo,不然还得多建几个类
static class User {
int age;
String name;
public User(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "User{" + "age=" + age + ", name='" + name + ''' + '}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//静态内部类,方便写demo,不然还得多建几个类,实现了Comparable接口,泛型匹配UserC
static class UserC implements Comparable<UserC> {
int age;
String name;
public UserC() {
}
public UserC(int age, String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "User{" + "age=" + age + ", name='" + name + ''' + '}';
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(UserC o) {
//o1-o2降序(顺序),反之o2-o1则升序(反序).
int age = this.age - o.getAge();
if (age == 0) {
//String类重写了compareTo方法
return this.name.compareTo(o.getName());
}
return age;
}
}
}
发表评论