src/share/classes/java/util/Comparator.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2007, 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


  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 
 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)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
 121      * <tt>compare(x, z)&gt;0</tt>.<p>


   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


  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)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
 121      * <tt>compare(x, z)&gt;0</tt>.<p>