排序实际上针对的是对对象数组进行的排序处理,如果要对对象数组进行排序,对象所在的类一定要实现Comparable接口(可比较接口)并且覆写compareTo()方法,但如果这个类不是我们自己定义的,那就没办法比较了,所以这个方法很不灵活。
例:
Person.java
public class Person implements Comparable{
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
@Override
public int compareTo(Person o) {
//按年龄升序排序
return this.getAge() - o.getAge();
}
}
TestTreeSet.java
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
person1.setAge(20);
person1.setName("Tonny");
person2.setAge(25);
person2.setName("Jack");
person3.setAge(17);
person3.setName("Calm");
TreeSet treeSet = new TreeSet<>();
treeSet.add(person1);
treeSet.add(person2);
treeSet.add(person3);
//Person类实现可比较接口
for(Person person : treeSet){
System.out.println(person);
}
}
}
更为灵活的方法是通过实现Comparator类来新建一个比较器,然后通过该比较器对类进行排序。
Comparator是比较器接口
在TreeSet构造方法中传入比较器接口实现类的对象有三种方式:匿名内部类、Lambda表达式、方法引用
例:匿名内部类法
Person.java
public class Person {
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
TestTreeSet.java
import java.util.Comparator;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
person1.setAge(20);
person1.setName("Tonny");
person2.setAge(25);
person2.setName("Jack");
person3.setAge(17);
person3.setName("Calm");
//TreeSet构造方法中传入比较器接口实现类的对象
TreeSet treeSet = new TreeSet<>(new Comparator() {
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
});
treeSet.add(person1);
treeSet.add(person2);
treeSet.add(person3);
for(Person person : treeSet){
System.out.println(person);
}
}
}
Lambda表达式法
Person.java
public class Person {
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
TestTreeSet.java
import java.util.Comparator;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
person1.setAge(20);
person1.setName("Tonny");
person2.setAge(25);
person2.setName("Jack");
person3.setAge(17);
person3.setName("Calm");
//TreeSet构造方法中传入比较器接口实现类的对象
TreeSet treeSet = new TreeSet<>((o1, o2)->(o1.getAge() - o2.getAge()));
treeSet.add(person1);
treeSet.add(person2);
treeSet.add(person3);
for(Person person : treeSet){
System.out.println(person);
}
}
}
方法引用法
Person.java
public class Person {
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
TestTreeSet.java
import java.util.Comparator;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
person1.setAge(20);
person1.setName("Tonny");
person2.setAge(25);
person2.setName("Jack");
person3.setAge(17);
person3.setName("Calm");
//TreeSet构造方法中传入比较器接口实现类的对象
TreeSet treeSet = new TreeSet<>(TestTreeSet::compare);
treeSet.add(person1);
treeSet.add(person2);
treeSet.add(person3);
for(Person person : treeSet){
System.out.println(person);
}
}
public static int compare(Person o1, Person o2){
return o1.getAge() - o2.getAge();
}
}
Comparator与Comparable的区别
Comparable是排序接口,若一个类实现了Comparable接口,就;意味着“该类支持排序”。
而Comparator是比较器,若需要控制某个类的次序,可以建立一个“该类的比较器”来进行排序。
Comparator相当于一个“外部比较器”;Comparable相当于一个“内部比较器”。