comparable
比较this和其他对象实例,类需实现(implements)java.lang.Comparable接口,重写public int compareTo(Object o1)
comparator
比较2个不同的对象实例,比较器类需实现(implements)java.util.Comparator接口,重写public int compare(Object o1, Object o2)
java.lang.Comparable:int compareTo(Object o1)
比较this Object 和 o1 Object
返回值意义:
1.正数—this Object 大于 o1
2.负数—this Object 小于 o1
3.0—this Object 等于 o1
在排序中,如果当前对象实例this某字段大于 o1某字段,返回正数,即按从小到大排序(正序);
int compareTo(Employee o1){
if (this.age > o1.age) {
return 1;
} else if (this.age < o1.age) {
return -1;
} else {
return 0;
}
}
java.util.Comparator:int compare(Object o1, Object o2)
比较o1 object 和 o2 object
返回值意义:
1.正数—o1 大于 o2
2.负数—o2 小于 o2
3.0—o1 等于 o2
当o1.getSalary() 大于 o2.getSalary(),返回正数,则从小到大输出(正序);依此类推
//根据对象实例的Salary从小到大
public int compare(Employee o1, Employee o2) {
// TODO Auto-generated method stub
if(o1.getSalary() > o2.getSalary()){
return 1;
}else if(o1.getSalary() < o2.getSalary()){
return -1;
}else{
return 0;
}
}
区别:
comparable | comparator |
---|---|
Defined in java.lang.comparable | Definded in java.util.comparator |
override public int compareTo(Object o1) | override public int compare(Object o1, Object 02) |
java.util.Collections.sort(List) and java.util.Arrays.sort(Object[]) | java.util.Collections.sort(List, Comparator) and java.util.Arrays.sort(Object[], Comparator) |
Class whose objects to be sorted must implement this interface | Class whose objects to be sorted do not need to implement this interface |
代码实例:
comparable接口
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Human implements Comparable<Human> {
private String name;
private int age;
private double height;
public Human(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
// 年龄从小到大
@Override
public int compareTo(Human o) {
// TODO Auto-generated method stub
if (this.age > o.age) {
return 1;
} else if (this.age < o.age) {
return -1;
} else {
return 0;
}
}
}
public class TestHumanSort {
private static void printList(List<Human> list) {
System.out.println("name\tage\theight");
for (Human hm : list) {
System.out.println(hm.getName() + "\t" + hm.getAge() + "\t"
+ hm.getHeight());
}
}
public static void main(String[] args) {
List<Human> testHumanList = new ArrayList<Human>();
testHumanList.add(new Human("Bill", 16, 175));
testHumanList.add(new Human("John", 18, 170));
testHumanList.add(new Human("Jack", 15, 180));
Collections.sort(testHumanList);
printList(testHumanList);
}
}
输出:
name age height
Jack 15 180.0
Bill 16 175.0
John 18 170.0
comparator接口
import java.util.Comparator;
public class EmpSortBySalary implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
// TODO Auto-generated method stub
if(o1.getSalary() > o2.getSalary()){
return 1;
}else if(o1.getSalary() < o2.getSalary()){
return -1;
}else{
return 0;
}
}
}
主函数:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
class Employee{
private String name;
private int age;
private long salary;
public Employee(String name, int age, long salary){
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
}
public class TestEmployeeSort {
private static void printList(List<Employee> list) {
System.out.println("name\tage\tsalary");
for (Employee ey : list) {
System.out.println(ey.getName() + "\t" + ey.getAge() + "\t"
+ ey.getSalary());
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Employee> emList = new ArrayList<Employee>();
emList.add(new Employee("Jack", 28, 10000));
emList.add(new Employee("John", 60, 3000));
emList.add(new Employee("Bill", 50, 20000));
Collections.sort(emList, new EmpSortBySalary());
printList(emList);
}
}
输出
name age salary
John 60 3000
Jack 28 10000
Bill 50 20000