1 /* 2 * @(#)Comparator.java 1.22 03/12/19 3 * 4 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 5 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 6 */ 7 8 package java.util; 9 10 /** 11 * A comparison function, which imposes a <i>total ordering</i> on some 12 * collection of objects. Comparators can be passed to a sort method (such as 13 * <tt>Collections.sort</tt>) to allow precise control over the sort order. 14 * Comparators can also be used to control the order of certain data 15 * structures (such as <tt>TreeSet</tt> or <tt>TreeMap</tt>).<p> 16 * 17 * The ordering imposed by a Comparator <tt>c</tt> on a set of elements 18 * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if 19 * <tt>(compare((Object)e1, (Object)e2)==0)</tt> has the same boolean value as 20 * <tt>e1.equals((Object)e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in 21 * <tt>S</tt>.<p> 22 * 23 * Caution should be exercised when using a comparator capable of imposing an 24 * ordering inconsistent with equals to order a sorted set (or sorted map). 25 * Suppose a sorted set (or sorted map) with an explicit Comparator <tt>c</tt> 26 * is used with elements (or keys) drawn from a set <tt>S</tt>. If the 27 * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals, 28 * the sorted set (or sorted map) will behave "strangely." In particular the 29 * sorted set (or sorted map) will violate the general contract for set (or 30 * map), which is defined in terms of <tt>equals</tt>.<p> 31 * 32 * For example, if one adds two keys <tt>a</tt> and <tt>b</tt> such that 33 * <tt>(a.equals((Object)b) && c.compare((Object)a, (Object)b) != 0)</tt> to a 34 * sorted set with comparator <tt>c</tt>, the second <tt>add</tt> operation 35 * will return false (and the size of the sorted set will not increase) 36 * because <tt>a</tt> and <tt>b</tt> are equivalent from the sorted set's 37 * perspective.<p> 38 * 39 * Note: It is generally a good idea for comparators to implement 40 * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in 41 * serializable data structures (like <tt>TreeSet</tt>, <tt>TreeMap</tt>). In 42 * order for the data structure to serialize successfully, the comparator (if 43 * provided) must implement <tt>Serializable</tt>.<p> 44 * 45 * For the mathematically inclined, the <i>relation</i> that defines 46 * the <i>total order</i> that a given comparator <tt>c</tt> imposes on a 47 * given set of objects <tt>S</tt> is:<pre> 48 * {(x, y) such that c.compare((Object)x, (Object)y) <= 0}. 49 * </pre> The <i>quotient</i> for this total order is:<pre> 50 * {(x, y) such that c.compare((Object)x, (Object)y) == 0}. 51 * </pre> 52 * 53 * It follows immediately from the contract for <tt>compare</tt> that the 54 * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the 55 * natural ordering is a <i>total order</i> on <tt>S</tt>. When we say that 56 * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with 57 * equals</i>, we mean that the quotient for the natural ordering is the 58 * equivalence relation defined by the objects' <tt>equals(Object)</tt> 59 * method(s):<pre> 60 * {(x, y) such that x.equals((Object)y)}. 61 * </pre><p> 62 * 63 * This interface is a member of the 64 * <a HREF="{@docRoot}/../guide/collections/index.html"> 65 * Java Collections Framework</a>. 66 * 67 * @author Josh Bloch 68 * @author Neal Gafter 69 * @version 1.22, 12/19/03 70 * @see Comparable 71 * @see Arrays#sort(Object[], Comparator) 72 * @see TreeMap 73 * @see TreeSet 74 * @see SortedMap 75 * @see SortedSet 76 * @see java.io.Serializable 77 * @since 1.2 78 */ 79 80 public interface Comparator<T> { 81 /** 82 * Compares its two arguments for order. Returns a negative integer, 83 * zero, or a positive integer as the first argument is less than, equal 84 * to, or greater than the second.<p> 85 * 86 * The implementor must ensure that <tt>sgn(compare(x, y)) == 87 * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This 88 * implies that <tt>compare(x, y)</tt> must throw an exception if and only 89 * if <tt>compare(y, x)</tt> throws an exception.)<p> 90 * 91 * The implementor must also ensure that the relation is transitive: 92 * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies 93 * <tt>compare(x, z)>0</tt>.<p> 94 * 95 * Finally, the implementer must ensure that <tt>compare(x, y)==0</tt> 96 * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all 97 * <tt>z</tt>.<p> 98 * 99 * It is generally the case, but <i>not</i> strictly required that 100 * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking, 101 * any comparator that violates this condition should clearly indicate 102 * this fact. The recommended language is "Note: this comparator 103 * imposes orderings that are inconsistent with equals." 104 * 105 * @param o1 the first object to be compared. 106 * @param o2 the second object to be compared. 107 * @return a negative integer, zero, or a positive integer as the 108 * first argument is less than, equal to, or greater than the 109 * second. 110 * @throws ClassCastException if the arguments' types prevent them from 111 * being compared by this Comparator. 112 */ 113 int compare(T o1, T o2); 114 115 /** 116 * 117 * Indicates whether some other object is "equal to" this 118 * Comparator. This method must obey the general contract of 119 * <tt>Object.equals(Object)</tt>. Additionally, this method can return 120 * <tt>true</tt> <i>only</i> if the specified Object is also a comparator 121 * and it imposes the same ordering as this comparator. Thus, 122 * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1, 123 * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference 124 * <tt>o1</tt> and <tt>o2</tt>.<p> 125 * 126 * Note that it is <i>always</i> safe <i>not</i> to override 127 * <tt>Object.equals(Object)</tt>. However, overriding this method may, 128 * in some cases, improve performance by allowing programs to determine 129 * that two distinct Comparators impose the same order. 130 * 131 * @param obj the reference object with which to compare. 132 * @return <code>true</code> only if the specified object is also 133 * a comparator and it imposes the same ordering as this 134 * comparator. 135 * @see java.lang.Object#equals(java.lang.Object) 136 * @see java.lang.Object#hashCode() 137 */ 138 boolean equals(Object obj); 139 } 140