Comparator 强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort。 int compare(Object o1, Object o2);Comparable 强行对实现它的每个类的对象进行整体排序,实现此接口的对象列表(和数组)可以通过Collections.sort或Arrays.sort进行自动排序。 int compareTo(Object o);
谢谢 izard999 前辈了
1. Classes should implement the Comparable interface to control their natural ordering.类通过实现Comparable接口来决定他们的自然排序实现了Comparable的对象能通过 Collections.sort() 和 Arrays.sort() 进行排序 而且可以作为sorted map的key和sorted set的元素,不需要指定Comparatorand can be used as keys in a or elements in a without the need to specify a Comparator.« Interface » Comparable + compareTo(Object) : int 2. 用Comparator可以以自然顺序之外的规则来排序对象« Interface » Comparator + compare(Object, Object) : int + equals(Object) : boolean
强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort。
int compare(Object o1, Object o2);Comparable
强行对实现它的每个类的对象进行整体排序,实现此接口的对象列表(和数组)可以通过Collections.sort或Arrays.sort进行自动排序。
int compareTo(Object o);
Classes should implement the Comparable interface to control their natural ordering.类通过实现Comparable接口来决定他们的自然排序实现了Comparable的对象能通过 Collections.sort() 和 Arrays.sort() 进行排序
而且可以作为sorted map的key和sorted set的元素,不需要指定Comparatorand can be used as keys in a or elements in a without the need to specify a Comparator.« Interface »
Comparable
+ compareTo(Object) : int
2.
用Comparator可以以自然顺序之外的规则来排序对象« Interface »
Comparator
+ compare(Object, Object) : int
+ equals(Object) : boolean