1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 /** 29 * A comparison function, which imposes a <i>total ordering</i> on some 30 * collection of objects. Comparators can be passed to a sort method (such 31 * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link 32 * Arrays#sort(Object[],Comparator) Arrays.sort}) to allow precise control 33 * over the sort order. Comparators can also be used to control the order of 34 * certain data structures (such as {@link SortedSet sorted sets} or {@link 35 * SortedMap sorted maps}), or to provide an ordering for collections of 36 * objects that don't have a {@link Comparable natural ordering}.<p> 37 * 38 * The ordering imposed by a comparator <tt>c</tt> on a set of elements 39 * <tt>S</tt> is said to be <i>consistent with equals</i> if and only if 40 * <tt>c.compare(e1, e2)==0</tt> has the same boolean value as 41 * <tt>e1.equals(e2)</tt> for every <tt>e1</tt> and <tt>e2</tt> in 42 * <tt>S</tt>.<p> 43 * 44 * Caution should be exercised when using a comparator capable of imposing an 45 * ordering inconsistent with equals to order a sorted set (or sorted map). 46 * Suppose a sorted set (or sorted map) with an explicit comparator <tt>c</tt> 47 * is used with elements (or keys) drawn from a set <tt>S</tt>. If the 48 * ordering imposed by <tt>c</tt> on <tt>S</tt> is inconsistent with equals, 49 * the sorted set (or sorted map) will behave "strangely." In particular the 50 * sorted set (or sorted map) will violate the general contract for set (or 51 * map), which is defined in terms of <tt>equals</tt>.<p> 52 * 53 * For example, suppose one adds two elements {@code a} and {@code b} such that 54 * {@code (a.equals(b) && c.compare(a, b) != 0)} 55 * to an empty {@code TreeSet} with comparator {@code c}. 56 * The second {@code add} operation will return 57 * true (and the size of the tree set will increase) because {@code a} and 58 * {@code b} are not equivalent from the tree set's perspective, even though 59 * this is contrary to the specification of the 60 * {@link Set#add Set.add} method.<p> 61 * 62 * Note: It is generally a good idea for comparators to also implement 63 * <tt>java.io.Serializable</tt>, as they may be used as ordering methods in 64 * serializable data structures (like {@link TreeSet}, {@link TreeMap}). In 65 * order for the data structure to serialize successfully, the comparator (if 66 * provided) must implement <tt>Serializable</tt>.<p> 67 * 68 * For the mathematically inclined, the <i>relation</i> that defines the 69 * <i>imposed ordering</i> that a given comparator <tt>c</tt> imposes on a 70 * given set of objects <tt>S</tt> is:<pre> 71 * {(x, y) such that c.compare(x, y) <= 0}. 72 * </pre> The <i>quotient</i> for this total order is:<pre> 73 * {(x, y) such that c.compare(x, y) == 0}. 74 * </pre> 75 * 76 * It follows immediately from the contract for <tt>compare</tt> that the 77 * quotient is an <i>equivalence relation</i> on <tt>S</tt>, and that the 78 * imposed ordering is a <i>total order</i> on <tt>S</tt>. When we say that 79 * the ordering imposed by <tt>c</tt> on <tt>S</tt> is <i>consistent with 80 * equals</i>, we mean that the quotient for the ordering is the equivalence 81 * relation defined by the objects' {@link Object#equals(Object) 82 * equals(Object)} method(s):<pre> 83 * {(x, y) such that x.equals(y)}. </pre> 84 * 85 * <p>Unlike {@code Comparable}, a comparator may optionally permit 86 * comparison of null arguments, while maintaining the requirements for 87 * an equivalence relation. 88 * 89 * <p>This interface is a member of the 90 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 91 * Java Collections Framework</a>. 92 * 93 * @param <T> the type of objects that may be compared by this comparator 94 * 95 * @author Josh Bloch 96 * @author Neal Gafter 97 * @see Comparable 98 * @see java.io.Serializable 99 * @since 1.2 100 */ 101 @FunctionalInterface 102 public interface Comparator<T> { 103 /** 104 * Compares its two arguments for order. Returns a negative integer, 105 * zero, or a positive integer as the first argument is less than, equal 106 * to, or greater than the second.<p> 107 * 108 * In the foregoing description, the notation 109 * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical 110 * <i>signum</i> function, which is defined to return one of <tt>-1</tt>, 111 * <tt>0</tt>, or <tt>1</tt> according to whether the value of 112 * <i>expression</i> is negative, zero or positive.<p> 113 * 114 * The implementor must ensure that <tt>sgn(compare(x, y)) == 115 * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This 116 * implies that <tt>compare(x, y)</tt> must throw an exception if and only 117 * if <tt>compare(y, x)</tt> throws an exception.)<p> 118 * 119 * The implementor must also ensure that the relation is transitive: 120 * <tt>((compare(x, y)>0) && (compare(y, z)>0))</tt> implies 121 * <tt>compare(x, z)>0</tt>.<p> 122 * 123 * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt> 124 * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all 125 * <tt>z</tt>.<p> 126 * 127 * It is generally the case, but <i>not</i> strictly required that 128 * <tt>(compare(x, y)==0) == (x.equals(y))</tt>. Generally speaking, 129 * any comparator that violates this condition should clearly indicate 130 * this fact. The recommended language is "Note: this comparator 131 * imposes orderings that are inconsistent with equals." 132 * 133 * @param o1 the first object to be compared. 134 * @param o2 the second object to be compared. 135 * @return a negative integer, zero, or a positive integer as the 136 * first argument is less than, equal to, or greater than the 137 * second. 138 * @throws NullPointerException if an argument is null and this 139 * comparator does not permit null arguments 140 * @throws ClassCastException if the arguments' types prevent them from 141 * being compared by this comparator. 142 */ 143 int compare(T o1, T o2); 144 145 /** 146 * Indicates whether some other object is "equal to" this 147 * comparator. This method must obey the general contract of 148 * {@link Object#equals(Object)}. Additionally, this method can return 149 * <tt>true</tt> <i>only</i> if the specified object is also a comparator 150 * and it imposes the same ordering as this comparator. Thus, 151 * <code>comp1.equals(comp2)</code> implies that <tt>sgn(comp1.compare(o1, 152 * o2))==sgn(comp2.compare(o1, o2))</tt> for every object reference 153 * <tt>o1</tt> and <tt>o2</tt>.<p> 154 * 155 * Note that it is <i>always</i> safe <i>not</i> to override 156 * <tt>Object.equals(Object)</tt>. However, overriding this method may, 157 * in some cases, improve performance by allowing programs to determine 158 * that two distinct comparators impose the same order. 159 * 160 * @param obj the reference object with which to compare. 161 * @return <code>true</code> only if the specified object is also 162 * a comparator and it imposes the same ordering as this 163 * comparator. 164 * @see Object#equals(Object) 165 * @see Object#hashCode() 166 */ 167 boolean equals(Object obj); 168 }