Comparable
Comparable可以认为是一个内比较器。可以和相同类型的对象作比较,也可以和不同类型的对象做比较
package com.hao.laker.study.javabase.comparable_comparator;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Created by haojiahong on 17/3/25.
*/
public class MyObject implements Comparable<MyObject> {
@Getter
private String str;
public MyObject(String str) {
this.str = str;
}
@Override
public int compareTo(MyObject o) {
//在自己的类内部是可以看到str的
if (this.str.compareTo(o.str) > 0) {
return 1;
} else if (this.str.compareTo(o.str) == 0) {
return 0;
} else {
return -1;
}
}
public static void main(String[] args) {
MyObject o1 = new MyObject("b");
MyObject o2 = new MyObject("b");
MyObject o3 = new MyObject("a");
MyObject o4 = new MyObject("d");
System.out.println(o1.compareTo(o2));
System.out.println(o1.compareTo(o3));
System.out.println(o1.compareTo(o4));
//list最后会按照从小到大的顺序输出
List<MyObject> myObjects = new ArrayList<>();
myObjects.add(o1);
myObjects.add(o4);
myObjects.add(o3);
myObjects.add(o2);
Collections.sort(myObjects);
System.out.println(JSON.toJSONString(myObjects));
}
}
结果:
0
1
-1
[{"str":"a"},{"str":"b"},{"str":"b"},{"str":"d"}]
Comparator
Comparator可以认为是一个外比较器。有两种情况下会使用Comparator。
- 对象没有实现Comparable接口。但又想对两个对象进行比较。
- 对象实现了Comparable接口,但是comparaTo方法中的比较条件不满足需求。
package com.hao.laker.study.javabase.comparable_comparator;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Created by haojiahong on 17/3/25.
*/
public class MyObjectComparator implements Comparator<MyObject> {
@Override
public int compare(MyObject o1, MyObject o2) {
if (o1.getStr().compareTo(o2.getStr()) == 1) {
return 1;
} else if (o1.getStr().compareTo(o2.getStr()) == 0) {
return 0;
} else {
return -1;
}
}
public static void main(String[] args) {
MyObject o1 = new MyObject("b");
MyObject o2 = new MyObject("b");
MyObject o3 = new MyObject("a");
MyObject o4 = new MyObject("d");
MyObjectComparator comparator = new MyObjectComparator();
System.out.println(comparator.compare(o1, o2));
System.out.println(comparator.compare(o1, o3));
System.out.println(comparator.compare(o1, o4));
//同样是从小到大输出了
List<MyObject> myObjects = new ArrayList<>();
myObjects.add(o1);
myObjects.add(o2);
myObjects.add(o3);
myObjects.add(o4);
Collections.sort(myObjects, comparator);
System.out.println(JSON.toJSONString(myObjects));
}
}
结果
0
1
-1
[{"str":"a"},{"str":"b"},{"str":"b"},{"str":"d"}]
Arrays.sort()
import java.util.Arrays;
import java.util.Comparator;
class Animal{
int size;
}
class Dog extends Animal{
public Dog(int s){
size = s;
}
}
class Cat extends Animal{
public Cat(int s){
size = s;
}
}
class AnimalSizeComparator implements Comparator<Animal>{
@Override
public int compare(Animal o1, Animal o2) {
return o1.size - o2.size;
}
//in this way, all sub classes of Animal can use this comparator.
}
public class ArraySort {
public static void main(String[] args) {
Dog d1 = new Dog(2);
Dog d2 = new Dog(1);
Dog d3 = new Dog(3);
Dog[] dogArray = {d1, d2, d3};
printDogs(dogArray);
Arrays.sort(dogArray, new AnimalSizeComparator());
printDogs(dogArray);
System.out.println();
//when you have an array of Cat, same Comparator can be used.
Cat c1 = new Cat(2);
Cat c2 = new Cat(1);
Cat c3 = new Cat(3);
Cat[] catArray = {c1, c2, c3};
printDogs(catArray);
Arrays.sort(catArray, new AnimalSizeComparator());
printDogs(catArray);
}
public static void printDogs(Animal[] animals){
for(Animal a: animals)
System.out.print("size="+a.size + " ");
System.out.println();
}
}
输出结果为:
size=2 size=1 size=3
size=1 size=2 size=3
size=2 size=1 size=3
size=1 size=2 size=3
其中,Collections.sort()方法底层使用的是Arrys.sort()。
总的来说,从Arrays.sort()中你应该了解到:
generic(范型)——super
策略模式
归并排序——nlog(n)时间复杂度
java.util.Collections.sort(List<T>list, Comparator<?super T> c)类似于Arrays.sort
网友评论