JavaSE---Comparable

概述

  This interface imposes a total ordering on the objects of each class that implements it.

    实现Comparable接口的每个class都可以排序;    

  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.

    实现Comparable接口的class是 自然排序,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}).  

    实现Comparable的对象 所属的list或数组,可以通过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}.

    实现Comparable的对象 可以作为SortedMap、SortedSet的key(不需要指定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.  

    class C 2个对象e1, e2 的自然排序 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核心类库 的自然排序 与 equals 一致;

    一个例外是BigDecimal, new BigDecimal(4.0) 与 new BigDecimal(4.00)  compareTo = 0 、equals = true;

  Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

    compareTo() 左<右 -> 负数、左=右 -> 0、左>右 -> 正数;

  

posted on   anpeiyong  阅读(6)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-10-09 JavaSE---官方文档

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示