JavaComparator源码总结Comparator源码注释翻译和解析中英⽂对照版
版本
JDK8(JDK1.8)
Comparator接⼝重点
1.Comparator接⼝是⼀个函数式接⼝,⾥⾯只有⼀个虚⽅法compare(T o1, T o2),该接⼝表⽰⼀个⽐较器,⽽实现Comparable接⼝则表⽰其实现类是可⽐较的,Comparable接⼝的虚⽅法是compareTo(T o)
Comparable接⼝源码可以看我这篇位置
2pare(T o1, T o2) 返回值⼤于0,表⽰ o1 - o2 > 0,若o1,o2相等,则compare(T o1, T o2) 返回0
3.Comparator接⼝⽅法
注:then开头⽐较器都是⼆次⽐较器,在第⼀次调⽤thispare(T o1, T o2)两者相等,才会使⽤⼀个新的⽐较器来区分两者顺序,如果第⼀次都不相等,则不会调⽤后续⽐较器
⽅法名作⽤
int compare(T o1, T o2)返回 o1 - o2 的值
Comparator comparing(Function
keyExtractor)返回⼀个新的⽐较器,其⽐较规则先经过keyExtractor处理o1和o2后,再将处理后的值相减c⽐较⼤⼩
(类似相减,其实是调⽤compareTo)
Comparator comparing( Function keyExtractor, Comparator
keyComparator)返回⼀个新的⽐较器,其⽐较规则先经过keyExtractor处理o1和o2后,再将使⽤keyComparator⽐较
器的规则⽐较⼤⼩
Comparator comparingInt(ToIntFunction
keyExtractor)和上⾯comparing(Function keyExtractor) 差不多,只不过ToIntFunction处理后得到的值是Int,则
再两个Int相减
Comparator comparingLong(ToLongFunction
keyExtractor)和上⾯comparing(Function keyExtractor) 差不多,只不过ToLongFunction处理后得到的值是
Long,则再两个Long相减
Comparator comparingDouble(ToDoubleFunction
keyExtractor)和上⾯comparing(Function keyExtractor) 差不多,只不过ToDoubleFunction处理后得到的值是
Double,则再两个Double相减
Comparator naturalOrder()返回⼀个⾃然顺序⽐较器,⽐较规则为o1pareTo(o2),相当于o1-o2
Comparator reverseOrder()返回⼀个与⾃然顺序⽐较器相反的⽐较器,⽐较规则为o2pareTo(o1),相当于o2-o1
Comparato nullsFirst(Comparator
comparator)
返回⼀个空友好的⽐较器,该⽐较器认为null⼩于⾮空,两个null相等
Comparator nullsLast(Comparator
comparator)
返回⼀个空友好的⽐较器,该⽐较器认为null⼤于⾮空,两个null相等Comparator reversed()返回⼀个新的⽐较器,它的compare(T o1, T o2) 的作⽤变成返回 o2 - o1 的值,即相反
Comparator thenComparing(Comparator
other)返回⼀个新的⽐较器,当调⽤thispare(T o1, T o2)时o1和o2相等,再调⽤otherpare(T o1, T o2)⽐较o1,o2,如果第⼀步不相等则直接返回,下⾯也⼀样
Comparator thenComparing(Function
keyExtractor)当调⽤thispare(T o1, T o2)时o1和o2相等,上⾯的other⽐较器⽤以下步骤⽣成的⽐较
器替代,该⽐较器符合以下步骤,会先⽤keyExtractor处理o1,o2,然后直接把处理后的值相减
Comparator thenComparing(Function keyExtractor, Comparator
keyComparator)当调⽤thispare(T o1, T o2)时o1和o2相等,上⾯的other⽐较器⽤以下步骤⽣成的⽐较器替代,该⽐较器符合以下步骤,先⽤keyExtractor处理o1,o2,再调⽤keyComparator⽐较处理后的值
Comparator
thenComparingInt(ToIntFunction
keyExtractor)和上⾯ thenComparing(Function keyExtractor)差不多,只是 只不过ToIntFunction处理后得到的值
是Int,然后两个Int相减
Comparator thenComparingLong(ToLongFunction
keyExtractor)和上⾯ thenComparing(Function keyExtractor)差不多,只不过ToLongFunction处理后得到的值是
Long,则再两个Long相减
keyExtractor)
Comparator thenComparingDouble(ToDoubleFunction
keyExtractor)和上⾯ thenComparing(Function keyExtractor)差不多, 只不过ToDoubleFunction处理后得到的
值是Double,则再两个Double相减
⽅法名作⽤
Comparator接⼝源码
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 <i>total ordering</i> 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}.<p>
* ⼀种⽐较函数,它对某些对象集合施加总排序。
* 可以将⽐较器传递给排序⽅法(例如Collections#sort(List,Comparator)或
* Arrays#sort(Object[],Comparator)),以便精确控制排序顺序。
* ⽐较器还可⽤于控制某些数据结构的顺序(如SortedSet或SortedMap),
* 或为没有Comparable⾃然顺序的对象集合提供排序。
*
* The ordering imposed by a comparator {@code c} on a set of elements
* {@code S} is said to be <i>consistent with equals</i> if and only if
* {@code cpare(e1, e2)==0} has the same boolean value as
* {@code e1.equals(e2)} for every {@code e1} and {@code e2} in
* {@code S}.<p>
* 当且仅当cpare(e1,e2)具有与e1和e2中的每⼀个e1和e2相同的布尔值时,
* ⽐较器c对⼀组元素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 {@code c}
* is used with elements (or keys) drawn from a set {@code S}.  If the
* ordering imposed by {@code c} on {@code 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 {@code equals}.<p>
* 当使⽤⽐较器时,应谨慎使⽤,
* ⽐较器能够对排序集(或排序映射)施加与等于不⼀致的排序。
* 假设⼀个带有显式⽐较器c的排序集(或排序映射)与从集合S中提取的元素(或键)⼀起使⽤。
* 如果c对S施加的排序与等于不⼀致,则排序集(或排序映射)的⾏为将“奇怪”。
* 特别是排序集(或排序映射)将违反集合(或映射)的⼀般契约,
* 该契约是根据equals定义的。
*
*
*
*
*
* For example, suppose one adds two elements {@code a} and {@code b} such that
* {@code (a.equals(b) && cpare(a, b) != 0)}
* to an empty {@code TreeSet} with comparator {@code c}.
* The second {@code add} operation will return
* 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.<p>
* 例如,假设添加两个元素a和b,使得(a.equals(b) && cpare(a, b) != 0)
* 到带有⽐较器c的空TreeSet。第⼆个add操作将返回true(树集的⼤⼩将增加),
* 因为从树集的⾓度来看a和b并不等价,即使这与set#add⽅法的规范相反。
*
*
*
*
*
* Note: It is generally a good idea for comparators to also implement
* {@code 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 {@code Serializable}.<p>
* 注意:⽐较器也实现java.io.Serializable通常是⼀个好主意,
* 因为它们可以⽤作可序列化数据结构(如TreeSet、TreeMap)中的排序⽅法。
* 为了成功序列化数据结构,⽐较器(如果提供)必须实现Serializable
*
*
*
*
* For the mathematically inclined, the <i>relation</i> that defines the
* <i>imposed ordering</i> that a given comparator {@code c} imposes on a
* given set of objects {@code S} is:
* 对于数学上倾向的,定义给定⽐较器c对给定对象集S施加的强制顺序的关系为:
*
* <pre>
*      {(x, y) such that cpare(x, y) <= 0}.
* </pre>
* The <i>quotient</i> for this total order is:
* 此总顺序的商为:
* <pre>
*      {(x, y) such that cpare(x, y) == 0}.
* </pre>
*
* It follows immediately from the contract for {@code compare} that the
* quotient is an <i>equivalence relation</i> on {@code S}, and that the
* imposed ordering is a <i>total order</i> on {@code S}.  When we say that
* the ordering imposed by {@code c} on {@code S} is <i>consistent with
* equals</i>, we mean that the quotient for the ordering is the equivalence
* relation defined by the objects' {@link Object#equals(Object)
* equals(Object)} method(s):
* 紧接着从compare的契约可以看出,商是S上的等价关系,
* 并且强加的顺序是S上的总顺序。当我们说c对S施加的排序与等于⼀致时,
* 我们的意思是排序的商是由对象Object#equals(Object)⽅法定义的等价关系
*
*
*
* <pre>
*    {(x, y) such that x.equals(y)}.
* </pre>
*
* <p>Unlike {@code Comparable}, a comparator may optionally permit
* comparison of null arguments, while maintaining the requirements for
* an equivalence relation.
* 与Comparable不同,⽐较器可以选择允许⽐较空参数,同时保持对等价关系的要求。 *
* <p>This interface is a member of the
* <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
* Java Collections Framework</a>.
equals不等于*
* @param <T> the type of objects that may be compared by this comparator
* @param <T> 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<T> {
/**
* 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.<p>
* ⽐较其两个参数的顺序。返回负整数、零或正整数,
* 因为第⼀个参数⼩于、等于或⼤于第⼆个参数。
*
* The implementor must ensure that {@code sgn(compare(x, y)) ==
* -sgn(compare(y, x))} for all {@code x} and {@code y}.  (This
* implies that {@code compare(x, y)} must throw an exception if and only
* if {@code compare(y, x)} throws an exception.)<p>
* 实现者必须确保所有x和y的sgn(compare(x, y)) ==-sgn(compare(y, x))。
* (这意味着compare(x, y)必须在且仅在 compare(y, x)引发异常时引发异常。)
*
* The implementor must also ensure that the relation is transitive:
* {@code ((compare(x, y)>0) && (compare(y, z)>0))} implies
* {@code compare(x, z)>0}.<p>
* 实现者还必须确保关系是可传递的:
* ((compare(x, y)>0) && (compare(y, z)>0))意味着compare(x, z)>0
*
*
*
*
* Finally, the implementor must ensure that {@code compare(x, y)==0}
* implies that {@code sgn(compare(x, z))==sgn(compare(y, z))} for all
* {@code z}.<p>
* 最后,实现者必须确保compare(x, y)==0意味着所有z的
* sgn(compare(x, z))==sgn(compare(y, z))
*
*
*
* It is generally the case, but <i>not</i> strictly required that
* {@code (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."<p>
* 通常是这样的,但不是严格要求(compare(x, y)==0) == (x.equals(y))。
* ⼀般来说,任何违反这⼀条件的⽐较国都应明确指出这⼀事实。
* 推荐的语⾔是“注意:这个⽐较器强加的顺序与equals不⼀致。”
*
*
* In the foregoing description, the notation
* {@code sgn(}<i>expression</i>{@code )} designates the mathematical
* <i>signum</i> function, which is defined to return one of {@code -1},
* {@code 0}, or {@code 1} according to whether the value of
* <i>expression</i> is negative, zero, or positive, respectively.
* 在前⾯的描述中,符号sgn(expression)指定数学符号函数,
* 该函数被定义为根据表达式的值是负、零还是正分别返回-1、0或1中的⼀个。
*
* @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
*        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
* {@code true} <i>only</i> if the specified object is also a comparator
* and it imposes the same ordering as this comparator.  Thus,
* {@code comp1.equals(comp2)} implies that {@code sgn(comp1pare(o1,
* o2))==sgn(comp2pare(o1, o2))} for every object reference
* {@code o1} and {@code o2}.<p>
* 指⽰其他对象是否等于此⽐较器。
* 此⽅法必须遵守Object#equals(Object)的⼀般约定。
* 此外,仅当指定的对象也是⼀个⽐较器并且它施加了与此⽐较器相同的顺序时,
* 此⽅法才能返回true。因此,comp1.equals(comp2)意味着对于每个对象引⽤o1和o2都有    * sgn(comp1pare(o1, o2))==sgn(comp2pare(o1, o2))
* 。
*
*
*
* Note that it is <i>always</i> safe <i>not</i> to override
* {@code 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.
* 注意,不重写Object.equals(Object)总是安全的。
* 但是,在某些情况下,
* 重写此⽅法可以通过允许程序确定两个不同的⽐较器施加相同的顺序来提⾼性能。
*
* @param  obj  the reference object with which to compare.
* @return  {@code 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<T> reversed() {
verseOrder(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.
* 返回⼀个字典顺序⽐较器和另⼀个⽐较器。如果此Comparator认为两个元素相等,
* 即compare(a, b) == 0,则使⽤other确定顺序。
*
* <p>The returned comparator is serializable if the specified comparator
* is also serializable.
* 如果指定的⽐较器也可序列化,则返回的⽐较器可序列化。
*
* @apiNote

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。