这里主要介绍Comparator
Comparator和Comparable接口功能相似,用法有区别
通过name的length比较大小
@Data
public class Employeee {
private String name;
private double salary;
public Employeee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public static void main(String[] args) {
Employeee[] staff = new Employeee[3];
staff[0] = new Employeee("Sun Wukong", 130000);
staff[1] = new Employeee("Pig Bajie", 11000);
staff[2] = new Employeee("Sha Wujing", 12000);
Arrays.sort(staff, new Comparator<Employeee>() {
@Override
public int compare(Employeee o1, Employeee o2) {
return o1.name.length()-o2.name.length();
}
});
for(Employeee employee:staff){
System.out.println(employee.name+"::"+employee.salary);
}
}
}
也可以使用lambda表达式
@Data
public class Employeee {
private String name;
private double salary;
public Employeee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public static void main(String[] args) {
Employeee[] staff = new Employeee[3];
staff[0] = new Employeee("Sun Wukong", 130000);
staff[1] = new Employeee("Pig Bajie", 11000);
staff[2] = new Employeee("Sha Wujing", 12000);
Arrays.sort(staff, (o1,o2)->o1.name.length()-o2.name.length());
for(Employeee employee:staff){
System.out.println(employee.name+"::"+employee.salary);
}
}
}
也可以用Comparator.comparing( Function<? super T, ? extends U> keyExtractor)方法
@Data
public class Employeee {
private String name;
private double salary;
public Employeee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public static void main(String[] args) {
Employeee[] staff = new Employeee[3];
staff[0] = new Employeee("Sun Wukong", 130000);
staff[1] = new Employeee("Pig Bajie", 11000);
staff[2] = new Employeee("Sha Wujing", 12000);
Arrays.sort(staff,Comparator.comparing(Employeee::getName));
for(Employeee employee:staff){
System.out.println(employee.name+"::"+employee.salary);
}
}
}
而且可以对多个属性比较,如果第一次比较相同,就比较第二个属性,这里桑Employeee同name,那么就比较salary
public class Employeee {
private String name;
private double salary;
public Employeee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public static void main(String[] args) {
Employeee[] staff = new Employeee[3];
staff[0] = new Employeee("Pig Bajie", 130000);
staff[1] = new Employeee("Pig Bajie", 11000);
staff[2] = new Employeee("Pig Bajie", 12000);
Arrays.sort(staff,Comparator.comparing(Employeee::getName).thenComparing(Employeee::getSalary));
for(Employeee employee:staff){
System.out.println(employee.name+"::"+employee.salary);
}
}
}
还有一种使用Comparator的方法,让类直接implements Comparator<>
但是这种方法的泛型括号里不能写本类,而是比较的类型属性就是什么类型,比如比较name长度就是implements Comparator<String>,因为需要Arrays.sort(names,Comparator.comparing(String::length));的括号里的两个属性是同一种类型。
如果是本类,但是要比较类里面的属性,就做不到,只能如下这种:
public class LengthComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
public static void main(String[] args) {
String[] names = {"Sun Wukong", "Pig Bajie", "Sha Wujing"};
//Arrays.sort(names, new LengthComparator());
//Arrays.sort(names, (first,second)->first.length()-second.length());
Arrays.sort(names,Comparator.comparing(String::length));
for(String name:names){
System.out.println(name);
}
}
}