关于比较器:Comparable与挽救的比较器:Comparator

比较器:Comparable

数组实际上会分为普通数组与对象数组两类使用情况,如果是普通数组则可以直接根据数据的大小关系进行排序(调用Arrays.sort()排序)。而对象数组由于其本身存放的都是地址数据,是不可能依据大小关系来实现排序的,但是在Arrays类中依然重载了一个sort()方法(对象数组排序:public static void sort(Object[] a)),此方法可以直接针对于对象数组实现排序。但是如果要想使用此方法进行排序,那么必须有一个前提:对象所在的类一定要实现Comparable接口,否则代码执行时会出现ClassCastException异常。而Comparable接口就属于比较器的一种,此接口定义如下:

public interface Comparable {
	public int compareTo(T o);
}

实现对象数组排序:

import java.util.Arrays;
class Book implements Comparable {// 实现比较器
	private String title ;
	private double price ;
	public Book(String title,double price) {
		this.title = title ;
		this.price = price ;
	}
	@Override
	public String toString() {
		return "书名:" + this.title + ",价格:" + this.price + "\n" ;
	}
	@Override
	public int compareTo(Book o) {	// Arrays.sort()会自动调用此方法比较
		if (this.price > o.price) {
			return 1 ; 
		} else if (this.price < o.price) {
			return -1 ;
		} else {
			return 0; 
		}
	}
}

public class TestDemo {
    public static void main(String[] args) throws Exception {
        Book books [] = new Book [] {
                 new Book("经典1",79.8) ,
                 new Book("经典2",69.8) ,
                 new Book("经典3",99.8) ,
                 new Book("经典4",89.8) 
        } ;
        Arrays.sort(books);						// 对象数组排序
        System.out.println(Arrays.toString(books));
    }
}

挽救的比较器:Comparator

利用Comparable接口实现的比较器是属于常见的用法,但是从另外一个角度来讲,如果要使用Comparable比较器,那么就意味着在类定义时就必须考虑好排序的需求。但是如果说现在某一个类定义的时候并没有实现Comparable接口,可是在不能修改类定义时又需要进行对象数组排序该怎么办呢?为此,在Java中又提供了另外一种比较器:Comparator接口(挽救的比较器),此接口定义如下:

@FunctionalInterface
public interface Comparator {
	public int compare(T o1, T o2);
	public boolean equals(Object obj);
}

 通过定义可以发现,在Comparator接口上使用了 "@FunctionalInterface" 注解声明,所以此接口为一个函数式接口.该接口提供了一个compare()方法,此方法的返回结果为: 1(>0),-1(<0),0(=0).

函数式接口不是应该只有一个方法吗?这个接口为什么定义了两个?

因为:Object类中的方法不属于限定范围.虽然Comparator接口中定义了两个抽象方法,但是子类真正在覆写时只需要覆写compare()方法即可,而equals()这样的方法在Object类中已经有默认实现(地址除外).  另外Comparator接口定义之初只定义了compare()与equals()两个抽象方法,而在JDK1.8之后,Comparator接口中处理抽象方法外也定义了default和static的普通方法.

 jdk1.8中的Cmparator接口:

package java.util;

import java.io.Serializable;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;
import java.util.Comparators;

/**
 * A comparison function, which imposes a total ordering on some
 * collection of objects.  Comparators can be passed to a sort method (such
 * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link
 * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control
 * over the sort order.  Comparators can also be used to control the order of
 * certain data structures (such as {@link SortedSet sorted sets} or {@link
 * SortedMap sorted maps}), or to provide an ordering for collections of
 * objects that don't have a {@link Comparable natural ordering}.

* * The ordering imposed by a comparator c on a set of elements * S is said to be consistent with equals if and only if * c.compare(e1, e2)==0 has the same boolean value as * e1.equals(e2) for every e1 and e2 in * S.

* * Caution should be exercised when using a comparator capable of imposing an * ordering inconsistent with equals to order a sorted set (or sorted map). * Suppose a sorted set (or sorted map) with an explicit comparator c * is used with elements (or keys) drawn from a set S. If the * ordering imposed by c on S is inconsistent with equals, * the sorted set (or sorted map) will behave "strangely." In particular the * sorted set (or sorted map) will violate the general contract for set (or * map), which is defined in terms of equals.

* * For example, suppose one adds two elements {@code a} and {@code b} such that * {@code (a.equals(b) && c.compare(a, b) != 0)} * to an empty {@code TreeSet} with comparator {@code c}. * The second {@code add} operation will return * true (and the size of the tree set will increase) because {@code a} and * {@code b} are not equivalent from the tree set's perspective, even though * this is contrary to the specification of the * {@link Set#add Set.add} method.

* * Note: It is generally a good idea for comparators to also implement * java.io.Serializable, as they may be used as ordering methods in * serializable data structures (like {@link TreeSet}, {@link TreeMap}). In * order for the data structure to serialize successfully, the comparator (if * provided) must implement Serializable.

* * For the mathematically inclined, the relation that defines the * imposed ordering that a given comparator c imposes on a * given set of objects S is:

 *       {(x, y) such that c.compare(x, y) <= 0}.
 * 
The quotient for this total order is:
 *       {(x, y) such that c.compare(x, y) == 0}.
 * 
* * It follows immediately from the contract for compare that the * quotient is an equivalence relation on S, and that the * imposed ordering is a total order on S. When we say that * the ordering imposed by c on S is consistent with * equals, we mean that the quotient for the ordering is the equivalence * relation defined by the objects' {@link Object#equals(Object) * equals(Object)} method(s):
 *     {(x, y) such that x.equals(y)}. 
* *

Unlike {@code Comparable}, a comparator may optionally permit * comparison of null arguments, while maintaining the requirements for * an equivalence relation. * *

This interface is a member of the * * Java Collections Framework. * * @param the type of objects that may be compared by this comparator * * @author Josh Bloch * @author Neal Gafter * @see Comparable * @see java.io.Serializable * @since 1.2 */ @FunctionalInterface public interface Comparator { /** * Compares its two arguments for order. Returns a negative integer, * zero, or a positive integer as the first argument is less than, equal * to, or greater than the second.

* * In the foregoing description, the notation * sgn(expression) designates the mathematical * signum function, which is defined to return one of -1, * 0, or 1 according to whether the value of * expression is negative, zero or positive.

* * The implementor must ensure that sgn(compare(x, y)) == * -sgn(compare(y, x)) for all x and y. (This * implies that compare(x, y) must throw an exception if and only * if compare(y, x) throws an exception.)

* * The implementor must also ensure that the relation is transitive: * ((compare(x, y)>0) && (compare(y, z)>0)) implies * compare(x, z)>0.

* * Finally, the implementor must ensure that compare(x, y)==0 * implies that sgn(compare(x, z))==sgn(compare(y, z)) for all * z.

* * It is generally the case, but not strictly required that * (compare(x, y)==0) == (x.equals(y)). Generally speaking, * any comparator that violates this condition should clearly indicate * this fact. The recommended language is "Note: this comparator * imposes orderings that are inconsistent with equals." * * @param o1 the first object to be compared. * @param o2 the second object to be compared. * @return a negative integer, zero, or a positive integer as the * first argument is less than, equal to, or greater than the * second. * @throws NullPointerException if an argument is null and this * comparator does not permit null arguments * @throws ClassCastException if the arguments' types prevent them from * being compared by this comparator. */ int compare(T o1, T o2); /** * Indicates whether some other object is "equal to" this * comparator. This method must obey the general contract of * {@link Object#equals(Object)}. Additionally, this method can return * true only if the specified object is also a comparator * and it imposes the same ordering as this comparator. Thus, * comp1.equals(comp2) implies that sgn(comp1.compare(o1, * o2))==sgn(comp2.compare(o1, o2)) for every object reference * o1 and o2.

* * Note that it is always safe not to override * Object.equals(Object). However, overriding this method may, * in some cases, improve performance by allowing programs to determine * that two distinct comparators impose the same order. * * @param obj the reference object with which to compare. * @return true only if the specified object is also * a comparator and it imposes the same ordering as this * comparator. * @see Object#equals(Object) * @see Object#hashCode() */ boolean equals(Object obj); /** * Returns a comparator that imposes the reverse ordering of this * comparator. * * @return a comparator that imposes the reverse ordering of this * comparator. * @since 1.8 */ default Comparator reversed() { return Collections.reverseOrder(this); } /** * Returns a lexicographic-order comparator with another comparator. * If this {@code Comparator} considers two elements equal, i.e. * {@code compare(a, b) == 0}, {@code other} is used to determine the order. * *

The returned comparator is serializable if the specified comparator * is also serializable. * * @apiNote * For example, to sort a collection of {@code String} based on the length * and then case-insensitive natural ordering, the comparator can be * composed using following code, * *

{@code
     *     Comparator cmp = Comparator.comparingInt(String::length)
     *             .thenComparing(String.CASE_INSENSITIVE_ORDER);
     * }
* * @param other the other comparator to be used when this comparator * compares two objects that are equal. * @return a lexicographic-order comparator composed of this and then the * other comparator * @throws NullPointerException if the argument is null. * @since 1.8 */ default Comparator thenComparing(Comparator other) { Objects.requireNonNull(other); return (Comparator & Serializable) (c1, c2) -> { int res = compare(c1, c2); return (res != 0) ? res : other.compare(c1, c2); }; } /** * Returns a lexicographic-order comparator with a function that * extracts a key to be compared with the given {@code Comparator}. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparing(keyExtractor, cmp))}. * * @param the type of the sort key * @param keyExtractor the function used to extract the sort key * @param keyComparator the {@code Comparator} used to compare the sort key * @return a lexicographic-order comparator composed of this comparator * and then comparing on the key extracted by the keyExtractor function * @throws NullPointerException if either argument is null. * @see #comparing(Function, Comparator) * @see #thenComparing(Comparator) * @since 1.8 */ default Comparator thenComparing( Function keyExtractor, Comparator keyComparator) { return thenComparing(comparing(keyExtractor, keyComparator)); } /** * Returns a lexicographic-order comparator with a function that * extracts a {@code Comparable} sort key. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparing(keyExtractor))}. * * @param the type of the {@link Comparable} sort key * @param keyExtractor the function used to extract the {@link * Comparable} sort key * @return a lexicographic-order comparator composed of this and then the * {@link Comparable} sort key. * @throws NullPointerException if the argument is null. * @see #comparing(Function) * @see #thenComparing(Comparator) * @since 1.8 */ default > Comparator thenComparing( Function keyExtractor) { return thenComparing(comparing(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that * extracts a {@code int} sort key. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparingInt(keyExtractor))}. * * @param keyExtractor the function used to extract the integer sort key * @return a lexicographic-order comparator composed of this and then the * {@code int} sort key * @throws NullPointerException if the argument is null. * @see #comparingInt(ToIntFunction) * @see #thenComparing(Comparator) * @since 1.8 */ default Comparator thenComparingInt(ToIntFunction keyExtractor) { return thenComparing(comparingInt(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that * extracts a {@code long} sort key. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparingLong(keyExtractor))}. * * @param keyExtractor the function used to extract the long sort key * @return a lexicographic-order comparator composed of this and then the * {@code long} sort key * @throws NullPointerException if the argument is null. * @see #comparingLong(ToLongFunction) * @see #thenComparing(Comparator) * @since 1.8 */ default Comparator thenComparingLong(ToLongFunction keyExtractor) { return thenComparing(comparingLong(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that * extracts a {@code double} sort key. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparingDouble(keyExtractor))}. * * @param keyExtractor the function used to extract the double sort key * @return a lexicographic-order comparator composed of this and then the * {@code double} sort key * @throws NullPointerException if the argument is null. * @see #comparingDouble(ToDoubleFunction) * @see #thenComparing(Comparator) * @since 1.8 */ default Comparator thenComparingDouble(ToDoubleFunction keyExtractor) { return thenComparing(comparingDouble(keyExtractor)); } /** * Returns a comparator that imposes the reverse of the natural * ordering. * *

The returned comparator is serializable and throws {@link * NullPointerException} when comparing {@code null}. * * @param the {@link Comparable} type of element to be compared * @return a comparator that imposes the reverse of the natural * ordering on {@code Comparable} objects. * @see Comparable * @since 1.8 */ public static > Comparator reverseOrder() { return Collections.reverseOrder(); } /** * Returns a comparator that compares {@link Comparable} objects in natural * order. * *

The returned comparator is serializable and throws {@link * NullPointerException} when comparing {@code null}. * * @param the {@link Comparable} type of element to be compared * @return a comparator that imposes the natural ordering on {@code * Comparable} objects. * @see Comparable * @since 1.8 */ @SuppressWarnings("unchecked") public static > Comparator naturalOrder() { return (Comparator) Comparators.NaturalOrderComparator.INSTANCE; } /** * Returns a null-friendly comparator that considers {@code null} to be * less than non-null. When both are {@code null}, they are considered * equal. If both are non-null, the specified {@code Comparator} is used * to determine the order. If the specified comparator is {@code null}, * then the returned comparator considers all non-null values to be equal. * *

The returned comparator is serializable if the specified comparator * is serializable. * * @param the type of the elements to be compared * @param comparator a {@code Comparator} for comparing non-null values * @return a comparator that considers {@code null} to be less than * non-null, and compares non-null objects with the supplied * {@code Comparator}. * @since 1.8 */ public static Comparator nullsFirst(Comparator comparator) { return new Comparators.NullComparator<>(true, comparator); } /** * Returns a null-friendly comparator that considers {@code null} to be * greater than non-null. When both are {@code null}, they are considered * equal. If both are non-null, the specified {@code Comparator} is used * to determine the order. If the specified comparator is {@code null}, * then the returned comparator considers all non-null values to be equal. * *

The returned comparator is serializable if the specified comparator * is serializable. * * @param the type of the elements to be compared * @param comparator a {@code Comparator} for comparing non-null values * @return a comparator that considers {@code null} to be greater than * non-null, and compares non-null objects with the supplied * {@code Comparator}. * @since 1.8 */ public static Comparator nullsLast(Comparator comparator) { return new Comparators.NullComparator<>(false, comparator); } /** * Accepts a function that extracts a sort key from a type {@code T}, and * returns a {@code Comparator} that compares by that sort key using * the specified {@link Comparator}. * *

The returned comparator is serializable if the specified function * and comparator are both serializable. * * @apiNote * For example, to obtain a {@code Comparator} that compares {@code * Person} objects by their last name ignoring case differences, * *

{@code
     *     Comparator cmp = Comparator.comparing(
     *             Person::getLastName,
     *             String.CASE_INSENSITIVE_ORDER);
     * }
* * @param the type of element to be compared * @param the type of the sort key * @param keyExtractor the function used to extract the sort key * @param keyComparator the {@code Comparator} used to compare the sort key * @return a comparator that compares by an extracted key using the * specified {@code Comparator} * @throws NullPointerException if either argument is null * @since 1.8 */ public static Comparator comparing( Function keyExtractor, Comparator keyComparator) { Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator); return (Comparator & Serializable) (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); } /** * Accepts a function that extracts a {@link java.lang.Comparable * Comparable} sort key from a type {@code T}, and returns a {@code * Comparator} that compares by that sort key. * *

The returned comparator is serializable if the specified function * is also serializable. * * @apiNote * For example, to obtain a {@code Comparator} that compares {@code * Person} objects by their last name, * *

{@code
     *     Comparator byLastName = Comparator.comparing(Person::getLastName);
     * }
* * @param the type of element to be compared * @param the type of the {@code Comparable} sort key * @param keyExtractor the function used to extract the {@link * Comparable} sort key * @return a comparator that compares by an extracted key * @throws NullPointerException if the argument is null * @since 1.8 */ public static > Comparator comparing( Function keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); } /** * Accepts a function that extracts an {@code int} sort key from a type * {@code T}, and returns a {@code Comparator} that compares by that * sort key. * *

The returned comparator is serializable if the specified function * is also serializable. * * @param the type of element to be compared * @param keyExtractor the function used to extract the integer sort key * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ public static Comparator comparingInt(ToIntFunction keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator & Serializable) (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); } /** * Accepts a function that extracts a {@code long} sort key from a type * {@code T}, and returns a {@code Comparator} that compares by that * sort key. * *

The returned comparator is serializable if the specified function is * also serializable. * * @param the type of element to be compared * @param keyExtractor the function used to extract the long sort key * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ public static Comparator comparingLong(ToLongFunction keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator & Serializable) (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); } /** * Accepts a function that extracts a {@code double} sort key from a type * {@code T}, and returns a {@code Comparator} that compares by that * sort key. * *

The returned comparator is serializable if the specified function * is also serializable. * * @param the type of element to be compared * @param keyExtractor the function used to extract the double sort key * @return a comparator that compares by an extracted key * @see #comparing(Function) * @throws NullPointerException if the argument is null * @since 1.8 */ public static Comparator comparingDouble(ToDoubleFunction keyExtractor) { Objects.requireNonNull(keyExtractor); return (Comparator & Serializable) (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); } }

利用Comparator接口实现对象数组排序:

class BookComparator implements java.util.Comparator {
	@Override
	public int compare(Book o1, Book o2) {
		if (o1.getPrice() > o2.getPrice()) {
			return 1 ;
		} else if (o1.getPrice() < o2.getPrice()) {
			return -1 ;
		} else {
			return 0;
		}
	}
}

修改测试程序:

public class TestDemo {
    public static void main(String[] args) throws Exception {
        Book books [] = new Book [] {
                 new Book("经典1",79.8) ,
                 new Book("经典2",69.8) ,
                 new Book("经典3",99.8) ,
                 new Book("经典4",89.8)  
        } ;
        java.util.Arrays.sort(books,(o1,o2)->{
            if (o1.getPrice() > o2.getPrice()) {
                return 1;
            } else if (o1.getPrice() < o2.getPrice()) {
                return -1;
            } else {
                return 0;
            }
        });
        System.out.println(java.util.Arrays.toString(books));
    }

 

你可能感兴趣的:(java语言)