首先我们看以一下各自的JDK源码:
在jdk1.8版本中Comparable接口仍然只有一个方法:compareTo ;
在jdk1.8版本中Comparator接口中增加了许多方法,接口也用了1.8才加进来的@FunctionalInterface注解。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Employee implements Comparable {
/**
* id是根据进入公司的先后顺序排序
*/
private int id;
/**
* 姓名
*/
private String name;
/**
* 职位
*/
private String position;
public Employee(int id, String name, String position) {
this.id = id;
this.name = name;
this.position = position;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public String toString() {
return "id[" + id + "],name[" + name + "],position[" + position + "]";
}
@Override
public int compareTo(Employee o) {
if (this.getId() > o.getId()) {
return 1;
} else if (this.getId() < o.getId()) {
return -1;
} else {
return 0;
}
}
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new Employee(10001, "张三", "Boss"));
list.add(new Employee(10006, "赵四", "Manager"));
list.add(new Employee(10035, "王五", "Manager"));
list.add(new Employee(10002, "李六", "staff"));
list.add(new Employee(10005, "马牛", "staff"));
// 按照id排序
Collections.sort(list);
// 如果要实现倒叙则调用下面的方法
//Collections.reverse(list);
for (Employee e : list) {
System.out.println(e + "id:" + e.getId());
}
}
}
输出:
id[10001],name[张三],position[Boss]id:10001
id[10002],name[李六],position[staff]id:10002
id[10005],name[马牛],position[staff]id:10005
id[10006],name[赵四],position[Manager]id:10006
id[10035],name[王五],position[Manager]id:10035
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class EmployComparator implements Comparator {
/**
* id是根据进入公司的先后顺序排序
*/
private int id;
/**
* 姓名
*/
private String name;
/**
* 职位
*/
private String position;
public EmployComparator() {
}
public EmployComparator(int id, String name, String position) {
this.id = id;
this.name = name;
this.position = position;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@Override
public int compare(EmployComparator o1, EmployComparator o2) {
if (o1.getId() > o2.getId()) {
return 1;
} else if (o1.getId() < o2.getId()) {
return -1;
} else {
return 0;
}
}
public static void main(String[] args) {
List list = new ArrayList();
list.add(new EmployComparator(1001, "张三", "Boss"));
list.add(new EmployComparator(1006, "赵四", "Manager"));
list.add(new EmployComparator(10035, "王五", "Manager"));
list.add(new EmployComparator(1002, "李六", "staff"));
list.add(new EmployComparator(1005, "马牛", "staff"));
// 按照id排序,也就是按照资历深浅排序,需要构造器
Collections.sort(list, new EmployComparator());
// 倒叙
//Collections.reverse(list);
for (EmployComparator e : list) {
System.out.println(e + "id:" + e.getId());
}
}
}
output:
id[1001],name[张三],position[Boss]id:1001
id[1002],name[李六],position[staff]id:1002
id[1005],name[马牛],position[staff]id:1005
id[1006],name[赵四],position[Manager]id:1006
id[10035],name[王五],position[Manager]id:10035
未完待续...
2019.5.20更新:
// 修改Employee排序
Comparator comparator = new Comparator() {
@Override
public int compare(Employee o1, Employee o2) {
if(o1.getName().hashCode() > o2.getName().hashCode()){
return 1;
}else if(o1.getName().hashCode() < o2.getName().hashCode()){
return -1;
}else {
return 0;
}
}
};
// 指定比较器
Collections.sort(list,comparator);
for (Employee e : list) {
System.out.println(e + "id:" + e.getId());
}
output:
d[10001],name[张三],position[Boss]id:10001
id[10002],name[李六],position[staff]id:10002
id[10035],name[王五],position[Manager]id:10035
id[10006],name[赵四],position[Manager]id:10006
id[10005],name[马牛],position[staff]id:10005