本文,先介绍Comparable 和Comparator两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明。
Comparable 简介
Comparable 是排序接口。
若一个类实现了Comparable接口,就意味着“该类支持排序”。 即然实现Comparable接口的类支持排序,假设现在存在“实现Comparable接口的类的对象的List列表(或数组)”,则该List列表(或数组)可以通过 Collections.sort(或 Arrays.sort)进行排序。
此外,“实现Comparable接口的类的对象”可以用作“有序映射(如TreeMap)”中的键或“有序集合(TreeSet)”中的元素,而不需要指定比较器。
1、Comparable的代码如下:
public interface Comparable<T> {public int compareTo(T o); }
2、Comparator的代码如下
public interface Comparator<T> {int compare(T o1, T o2);boolean equals(Object obj); // jdk1.8 后的方法default Comparator<T> reversed() {return Collections.reverseOrder(this); }default Comparator<T> thenComparing(Comparator<? super T> other) { Objects.requireNonNull(other);return (Comparator<T> & Serializable) (c1, c2) -> {int res = compare(c1, c2);return (res != 0) ? res : other.compare(c1, c2); }; }default <U> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) {return thenComparing(comparing(keyExtractor, keyComparator)); }default <U extends Comparable<? super U>> Comparator<T> thenComparing( Function<? super T, ? extends U> keyExtractor) {return thenComparing(comparing(keyExtractor)); }default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor) {return thenComparing(comparingInt(keyExtractor)); }default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor) {return thenComparing(comparingLong(keyExtractor)); }default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor) {return thenComparing(comparingDouble(keyExtractor)); }public static <T extends Comparable<? super T>> Comparator<T> reverseOrder() {return Collections.reverseOrder(); } @SuppressWarnings("unchecked")public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {return (Comparator<T>) Comparators.NaturalOrderComparator.INSTANCE; }public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) {return new Comparators.NullComparator<>(true, comparator); }public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) {return new Comparators.NullComparator<>(false, comparator); }public static <T, U> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator);return (Comparator<T> & Serializable) (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); }public static <T, U extends Comparable<? super U>> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor);return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); }public static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor);return (Comparator<T> & Serializable) (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); }public static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor);return (Comparator<T> & Serializable) (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); }public static<T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor) { Objects.requireNonNull(keyExtractor);return (Comparator<T> & Serializable) (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); } }
Comparable和Comparator的主要区别在于:
(1).Comparator 和 Comparable都是Java中的内部比较器接口,都是用来实现对一个自定义的类进行排序
(2). 不同的是实现Comparable接口是定义在类的内部,比较代码需要嵌入类的内部结构中
(3). Comparator 实现在类的外部,单独实现第一个比较器,不需要对原来的类进行结构上的变化,属于无侵入式的。
例如实现Comparable接口:
class Score implements Comparable<Score>{ public int score; public int time; public int getScore() { return score; } public void setScore(int score) { this.score = score; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } @Override public int compareTo(Score o) { if(this.time>o.time) return 1; else if(this.time==o.time) return 0; else return -1; } public Score(int score, int time) { super(); this.score = score; this.time = time; } }
实现Comparator接口的例子:
class ScoreComparator implements Comparator<Score>{ @Override public int compare(Score o1, Score o2) { if(o1.time>o2.time) return 1; else if(o1.time==o2.time) return 0; else return -1; } }
然后调用ScoreComparator :
Arrays.sort(score, new ScoreComparator());
以上是java中Comparator和 Comparable的介绍及区别实例的详细内容。更多信息请关注PHP中文网其他相关文章!