当对自己定义的数据结构需要进行指定排序的时候,java中给了我们两个接口Comparable和Comparator:
comparable接口定义一个方法:
public interface Comparable {
public int compareTo(T o);
}
comparator接口定义方法(JDK 1.8):
public interface Comparator {
int compare(T o1, T o2);
boolean equals(Object obj);
}
区别在于 : 实现了comparable的对象直接就可以成为一个可以比较的对象,不过得在类中进行方法定义;comparator在对象外比较,不修改实体类。
Comparable | Comparator | |
---|---|---|
接口所在包 | java.lang.Comparable |
java.util.Comparator |
排序区别 | 排序方法必须在待排序对象的类中,故称之为自然排序 | 不必再一个类中,其他任何一个方法内都可以 |
排序方法 | int compareTo(Object o1) |
int compare(Object o1,Object o2) |
排序的调用 | Collections.sort(List) |
Collections.sort(List, Comparator) |
代码实例:
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* @ClassName TestDemo2
* @Description Comparable
* @Author lzq
* @Date 2018/10/23 17:01
* @Version 1.0
**/
class Person implements Comparable {
private final String firstName,lastName;
public Person(String firstName,String lastName) {
if(firstName == null || lastName == null) {
throw new NullPointerException();
}
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public boolean equals(Object obj) {
if(!(obj instanceof Person)) {
return false;
}
Person person = (Person) obj;
return person.firstName.equals(firstName) && person.lastName.equals(lastName);
}
public int hashCode() {
return 31*lastName.hashCode()+lastName.hashCode();
}
public String toString() {
return firstName+" "+lastName;
}
public int compareTo(Person person) {
int lastCmp = lastName.compareTo(person.lastName);
return (lastCmp != 0 ? lastCmp : firstName.compareTo(person.firstName));
}
}
public class TestDemo2 {
public static void main(String[] args) {
Person[] array = {new Person("John","Len"),
new Person("Karl","Ma"),
new Person("Gr","Ma"),
new Person("Os","Gr")};
List personList = Arrays.asList(array);
Collections.sort(personList);
System.out.println(personList);
}
}
[Os Gr, John Len, Gr Ma, Karl Ma]
/**
* @ClassName TestDemo1
* @Description Comparator
* @Author lzq
* @Date 2018/10/23 16:35
* @Version 1.0
**/
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 职员类
*/
class Staff {
String name; //名字
int age; //年龄
double salary; //工资
public Staff(String name,int age,double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return this.name;
}
public int getAge() {
return age;
}
public double getSalary() {
return salary;
}
public String toString() { //没有这个方法后面打印出来的是地址
return name+" "+age+" "+salary;
}
}
public class TestDemo1 {
//使用匿名类型创建比较器比较名字
static final Comparator NAME = new Comparator() {
public int compare(Staff o1, Staff o2) {
return o1.name.compareTo(o2.name);
}
};
//使用匿名类型创建比较器比较年龄
static final Comparator AGE = new Comparator() {
public int compare(Staff o1, Staff o2) {
return (o1.getAge() < o2.getAge() ? -1 : (o1.getAge()) == o2.getAge() ? 0 : 1);
}
};
//使用匿名类型创建比较器比较工资
static final Comparator SALARY = new Comparator() {
/**
* 通过比较两个入参得到顺序。返回值有三种:
*
* 1,入参一大于入参二。
* 0,入参相同。
* -1,入参一小于入参二。
* @param o1
* @param o2
* @return
* */
public int compare(Staff o1, Staff o2) {
return (o1.getSalary() < o2.getSalary() ? -1 : (o1.getSalary()) == o2.getSalary() ? 0 : 1);
}
};
public static void main(String[] args) {
Staff staff[] = {new Staff("Mike",25,2200.00),
new Staff("Jack",35,3900.00),
new Staff("Jone",28,2900.00),
new Staff("Mary",45,3200.00),
new Staff("Tom",40,5200.00),};
List list = Arrays.asList(staff);
Collections.sort(list,NAME);
System.out.println("按名字排序");
System.out.println(list);
Collections.sort(list,AGE);
System.out.println("按年龄排序");
System.out.println(list);
Collections.sort(list,SALARY);
System.out.println("按工资排序");
System.out.println(list);
}
}
按名字排序
[Jack 35 3900.0, Jone 28 2900.0, Mary 45 3200.0, Mike 25 2200.0, Tom 40 5200.0]
按年龄排序
[Mike 25 2200.0, Jone 28 2900.0, Jack 35 3900.0, Tom 40 5200.0, Mary 45 3200.0]
按工资排序
[Mike 25 2200.0, Jone 28 2900.0, Mary 45 3200.0, Jack 35 3900.0, Tom 40 5200.0]