Source code analysis of comparable interface in jdk8

/**

  • Created by caoxiaohong on 17/11/18 23:14.
    */

import java.util.*;

/**

  • This interface imposes a total ordering on the objects of each class that

  • implements it. This ordering is referred to as the class's natural

  • ordering,and the class's compareTo method is referred to as

  • its natural comparison method.

  • 这一接口会对实现了它的类施加一个整体的顺序.这一顺序被认为是类的自然顺序,类的比较方法compareTo()也被认为是自然比较方法.

  • Lists (and arrays) of objects that implement this interface can be sorted

  • automatically by {@link Collections#sort(List) Collections.sort} (and

  • {@link Arrays#sort(Object[]) Arrays.sort}). Objects that implement this

  • interface can be used as keys in a {@linkplain SortedMap sorted map} or as

  • elements in a {@linkplain SortedSet sorted set},without the need to

  • specify a {@linkplain Comparator comparator}.

  • 实现类这一接口的List类对象使用Collections.sort方法实现自动排序(升序),数组使用Arrays.sort()方法实现升序排序.实现这一接口的对象在

  • 有序Map中,有序是按照key进行排序的;在有序Set中,是按照set集合中的元素排序的.而使用这些方法时,我们并不需要指定比较器comparator(说明:

  • 这些排序都是默认升序排序,且排序字段只有一个.如果一个类有多个排序字段,要对这个类集合进行排序,则需要重写比较器方法).

  • The natural ordering for a class C is said to be consistent

  • with equals if and only if e1.compareTo(e2) == 0 has

  • the same boolean value as e1.equals(e2) for every

  • e1 and e2 of class C. Note that null

  • is not an instance of any class,and e.compareTo(null) should

  • throw a NullPointerException even though e.equals(null)

  • returns false.

  • 对于类C的任意变量e1和e2,当且仅当e1.compareTo(e2) == 0的和e1.equals(e2)有相同的返回值时,类的自然排序才能被认为是和equals方法的

  • 结果保持一致的.

  • 注意:虽然e.equals(null)返回值为false,但是null不是任何类的实例,所以如果调用方法e.compareTo(null)应该抛出异常NullPointerException

  • It is strongly recommended (though not required) that natural orderings be

  • consistent with equals. This is so because sorted sets (and sorted maps)

  • without explicit comparators behave "strangely" when they are used with

  • elements (or keys) whose natural ordering is inconsistent with equals. In

  • particular,such a sorted set (or sorted map) violates the general contract

  • for set (or map),which is defined in terms of the equals

  • method.

  • 我们强烈建议(尽管并不是必须的):自然排序应该和equals结果保持一致(这是因为自然排序用到了compare方法,这里的意思是需要满足关系:

  • e1.compareTo(e2) == 0的和e1.equals(e2)有相同的返回值).这是因为没有明确比较器的有序set(和有序map)

  • (什么叫没有明确比较器?对于TreeSet和TreeMap,都有多个实例构造函数,而其中有一个无参构造函数,就指定了比较器comparator = null;同时,* 这也说明了,如果你想在建立有序set或者有序map时就指定它的排序方法,那么可以给构造函数传入一个比较器参数即可.),如果自然排序不能和equals方法

  • 保持一致,那么它们会表现出一些诡异的行为.而且,这样的有序set(或者map)和equals中通用规范是矛盾的。

  • For example,if one adds two keys a and b such that

  • (!a.equals(b) && a.compareTo(b) == 0) to a sorted

  • set that does not use an explicit comparator,the second add

  • operation returns false (and the size of the sorted set does not increase)

  • because a and b are equivalent from the sorted set's

  • perspective.

  • 举个例子:如果向一个没有明确比较器的有序set中添加2个值a和b(a.equals(b)值为false,而 a.compareTo(b) == 0 值为true(a==b)),* 那么第二次的add操作会失败,因为从有序set的角度看,a和b是等值的.(出现这种事情就很诡异了,明明a在add之后,b再add时,这是两个不同的值,应该被正常

  • 添加到集合中,但是却被拒绝了,因为add时,使用到了方法compare,去比较插入的值是否存在,而根据返回结果为0,这样二者就被认为是相同的值.所以

  • 我们一再强调:为避免这种异常,自然排序要和equals结果保持一致,必须满足e1.compareTo(e2) == 0的和e1.equals(e2)始终有相同的返回值)

  • Virtually all Java core classes that implement Comparable have natural

  • orderings that are consistent with equals. One exception is

  • java.math.BigDecimal,whose natural ordering equates

  • BigDecimal objects with equal values and different precisions

  • (such as 4.0 and 4.00).

  • 实质上,所有实现了Comparable接口的java核心类,都满足自然排序的要求.唯一的例外类是:BigDecimal类.它的自然排序要求是:值相等而精度是不等的.

  • 所以,精度不同但值相同的两个BigDecimal对象,它们的equals方法返回值应该为true,而compare()方法应该返回0:

  • 测试用例如下:

  • import java.math.BigDecimal;

  • public class testCode {
    public static void main(String[] args){
    BigDecimal a=new BigDecimal(2.30);
    BigDecimal b=new BigDecimal(2.3);
    System.out.println("a.equals(b): "+a.equals(b));
    System.out.println("a.compareTo(b): "+a.compareTo(b));
    }
    }

    输出结果:
    a.equals(b): true
    a.compareTo(b): 0

  • For the mathematically inclined,the relation that defines

  • the natural ordering on a given class C is:

  • {(x,y) such that x.compareTo(y) <= 0}.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>