Comparable and Comparator API

【0】README

0.1)文本全文翻译于 java 8 JDK 官方文档中关于 Comparable and Comparator 的描述;
0.2)能力有限,仅供参考;


【1】 Interface Comparable

1.0)类型参数: 与本对象进行比较的另一个对象的类型

1.1)这个接口对实现它的类对象进行整体排序。排序被称为自然排序,类方法 compareTo 被称为 自然比较方法;

1.2)实现了该接口的对象列表List 能够通过 Colletions.sort( 或者 Arrays.sort) 自动排序。实现该接口的 Object 可以作为 SortedMap 中的 键值或者 SortSet中的元素, 不需要指定比较器 Comparator;

1.3)要知道, 对类C的自然排序等同于 equals 方法, 也就是说, e1.compareTo(e2) ==0 与 e1.equals(e2) 有同样的结果。 注意到, null 不是 任何对象的实例, 所以 e.compareTo(null)
应该抛出 空指针异常, 即使 e.equals(null) 返回 false;

1.4)强烈推荐(即使不是硬性要求):自然排序应该与 equals 方法一致。 这样做的原因是,没有附带显式比较器 Comparator 的 SortedSet 和 SortedMap 执行效果很奇怪;特别地,这样一种
SortSet 或者 SortedMap 违背了 set 或者 map 的 由equals 方法定义的常规约定;

1.5)举个荔枝,向没有附带显式比较器Comparator的SortSet添加两个元素使得 (!a.equals(b) && a.compareTo(b) == 0),那么 第二个add 操作失败(), 因为a 和 b 等同于 SortedSet的
观点。

1.6)事实上, 所有实现了 Comparable 接口的java核心类 的 自然排序都和 equals 方法保持一致。其中一个例外是 java.math.BigDecimal, 它的自然排序是值相等,但精度不同
(如4.0 和 4.00);

1.7)从数学观点看, 对给定类C定义自然排序的联系是: {(x,y) 使得 x.compareTo(y) <= 0} , x 在 y 之前;

1.8)商(quotient )意味着 {(x,y) 使得 x.compareTo(y)}

1.9) int compareTo(T o) 方法

  • 强烈建议使用: (x.compareTo(y)==0) == (x.equals(y))

【2】Interface Comparator

2.0)T类型参数T:此比较器比较的对象类型

2.1)对一些集合对象施加整体排序的比较函数。传递 比较器Comparator(下同) 给 一个排序方法(如 Collections.sort 或者 Arrays.sort 方法)以允许对排序序列进行精确控制。也可以使用比较器控制某个数据结构的排序(如SortedSet 或者 SortedMap), 或者为那些没有自然排序的对象集合提供一个排序;

2.2)比较器C 施加给 元素集合S 的排序 被认为是和 equals 方法保持一致的。 当且仅当 c.compare(e1, e2) == 0 与 e1.equlas(e2) 返回同样的 boolean 结果;

2.3)要注意的是,使用的是 一个 对 equals 方法 不一致,但却对 集合 施加排序的这么一个比较器。 后面的叙述和 Comparable 差不多一样;……

注意: 一个好的做法是:比较器也实现了 java.io.Serializable 接口, 因为它们可能被用在 serializable 可序列化数据结构中(如 TreeSet 和 TreeMap)。要对某些数据结构序列化成功的话,比较器必须实现 Serializable (序列化接口);


【3】initial desc in API

public interface Comparable<T>
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.
Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a 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 ase1.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 aNullPointerException even though e.equals(null) returns false.
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 equalsmethod.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.
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 BigDecimalobjects with equal values and different precisions (such as 4.0 and 4.00).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 quotient for this total order is:{(x, y) such that x.compareTo(y) == 0}.It follows immediately from the contract for compareTo that the quotient is an equivalence relation on C, and that the natural ordering is a total order on C. When we say that a class's natural ordering isconsistent with equals, we mean that the quotient for the natural ordering is the equivalence relation defined by the class's equals(Object) method:{(x, y) such that x.equals(y)}. 
This interface is a member of the Java Collections Framework.

@FunctionalInterface
public interface Comparator<T>
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or 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 sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have anatural ordering.
The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 inS.
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 c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on 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 equals.
For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c. The second add operation will return true (and the size of the tree set will increase) because a and b are not equivalent from the tree set's perspective, even though this is contrary to the specification of the Set.add method.
Note: It is generally a good idea for comparators to also implement java.io.Serializable, as they may be used as ordering methods in serializable data structures (like TreeSet, TreeMap). In order for the data structure to serialize successfully, the comparator (if provided) must implement Serializable.......

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331727.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sql2008 sql服务_SQL即服务

sql2008 sql服务自2007年以来&#xff0c;我一直在考虑这一点&#xff0c;大约在Amazon 推出 S3时。 我什至尝试实现了几次&#xff0c;但是在设计阶段之后就失败了。 我听说过一家初创公司也尝试这样做&#xff0c;但也失败了 。 我仍然不确定是否可以这样做&#xff0c;但是它…

dede rss.php,DeDeCMS dede 织梦cms RSS全站静态输出的实现方法

DedeCMS自带Rss功能&#xff0c;在管理后台可以生成出一个Rss地图页面&#xff0c;默认是/data/rssmap.html&#xff1b;这个Html地图文件会告知用户每个栏目的rss订阅地址&#xff0c;这个功能很适合栏目及文章较多的网站&#xff0c;但不太适合文章页不多的小型站点&#xff…

java之注解

注解使用一、java的常用注解 Override 重写覆盖方法 SuppressWarnings 压住警告可用于类和方法 Deprecated 声明类或方法过时 注解Override用在方法上&#xff0c;当我们想重写一个方法时&#xff0c;在方法上加Overr…

java集合——队列和双端队列+优先级队列

【0】README 0.1&#xff09; 本文描述转自 core java volume 1&#xff0c; 源代码为原创&#xff0c;旨在理解 java集合——队列和双端队列优先级队列 的相关知识&#xff1b; 0.2&#xff09; for full source code , please visit https://github.com/pacosonTang/core-j…

java无效的源发行版_无效的Java

java无效的源发行版也许我可以被机器人代替进行代码审查。 有一些反馈我发现自己一遍又一遍。 这是我最不喜欢的一些&#xff1a; 通用代码结构 放弃其他 当if在两端return的else是多余的和不必要的创建缩进。 if (foo) { return bar; } else { return baz; } // should be…

oracle数值型转为char类型,PLSQL: Oracle函数to_char转化数字型指定小数点位数的技巧...

问题题出&#xff1a; 数字0.023 > 转化成字符串"0.023"问题难点&#xff1a;to_char&#xff0c;函数功能&#xff0c;就是将数值型或者日期型转化为字符型。比如最简单的应用&#xff1a;Select TO_CHAR(1.0123) FROM DUALSelect TO_CHAR(123) …

java之常用方法

一、数学函数 在System.Math类里面的函数 I、三角函数 sin(radians) 正弦函数 cos(radians) 余弦函数 tan(radians) 正切函数 toRadians(degree) 角度转为弧度 toDegree(radians) 弧度转为角度 asin(a) 反正弦函数 acos(a)…

java集合——映射表+专用集合映射表类

【0】README 0.1&#xff09; 本文描述转自 core java volume 1&#xff0c; 源代码为原创&#xff0c;旨在理解 java集合——映射表专用集合映射表类 的相关知识&#xff1b; 0.2&#xff09; for full source code , please visit https://github.com/pacosonTang/core-jav…

jstat分析_jstat –分析

jstat分析jstat是一个简单的实用工具&#xff0c;在JDK中存在&#xff0c;用于提供与JVM性能相关的统计信息&#xff0c;例如垃圾收集&#xff0c;编译活动。 jstat的主要优势在于&#xff0c;它可以在运行JVM且无需任何先决条件的情况下动态捕获这些指标。 这是什么意思&#…

oracle中execute函数,oracle Execute Immediate(sql语句)

慕尼黑的夜晚无繁华Example 1:Output:ABCDECLARE TYPE var_typ IS TABLE OF VARCHAR2(4000);cVars var_typ;cVar VARCHAR2(4000);BEGINEXECUTE IMMEDIATE SELECT A cc FROM dualUNIONSELECT B cc FROM dualUNIONSELECT C cc FROM dual BULK COLLECT INTO cVars; FOR i IN 1 .…

java之数组

一、声明数组 数组是引用类型&#xff0c;声明数组并没有分配空间&#xff0c;则该数组变量的值为null。 类型[] 数组名; 如&#xff1a;double[] data; 二、创建数组 类型[] 数组名new 类型[长度]; 数组创建&#xff0c;数组里面的元素会被赋值&#xff0c;数值型基本数据…

java集合——集合框架

【0】README 0.1&#xff09; 本文描述转自 core java volume 1&#xff0c; 源代码为原创&#xff0c;旨在理解 java集合——集合框架 的相关知识&#xff1b; 【1】集合框架 1.1&#xff09; java集合类库构成了集合类的矿建&#xff0c; 它为集合的实现者定义了大量的接口…

oracle密码不能重复用_重复码

oracle密码不能重复用介绍 在我们的Java应用程序中复制/粘贴代码通常不好&#xff0c;但是有时这是不可避免的。 例如&#xff0c;项目License3j在Feature类中为其支持的每种XXX类型提供了一个isXXX方法。 在这种情况下&#xff0c;我们做不到写 public boolean isBinary() { r…

oracle rman 用户,对Oracle数据库进行RMAN备份的Oracle数据库用户权限

在Oracle 12版本或者更高版本&#xff0c;Oracle备份用户需要具有SYSDBA或者SYSBACKUP权限在Oracle 11(包括11版本)以前&#xff0c;Oracle备份用户需要具有SYSDBA权限创建及授权用户权限命令示例如下&#xff1a;SQL>create user backupuser identified by oracle;SQL>g…

java之类

一、构造方法1)构造方法具有与类的相同名字2)构造方法没有返回值&#xff0c;甚至连void都没有3)创建新对象使用new&#xff0c;构造方法的作用就是初始化对象。格式为&#xff1a; [可见修饰符] 类名(参数表){}例如&#xff1a;public Myclass(){}普通方法&#xff1a; p…

pmml_再访PMML

pmml嗨伙计&#xff01; 从今年年初开始&#xff0c;就有了重新设计Drools PMML模块的计划。 在这篇文章中&#xff0c;我将描述我们将如何处理它&#xff0c;目前的状态&#xff0c;未来发展的想法等&#xff0c;等等……敬请期待&#xff01; 背景 PMML是一个标准&#xff…

java集合——视图与包装器

【0】README 0.1&#xff09; 本文描述转自 core java volume 1&#xff0c; 源代码为原创&#xff0c;旨在理解 java集合——视图与包装器 的相关知识&#xff1b; 0.2&#xff09; for full source code , please visit https://github.com/pacosonTang/core-java-volume/b…

php把表单转为json保存,javascript – 使用jquery将表单数据保存到本地json文件中

我有一个带有一些输入字段的基本表单.我想在提交表单时将表单数据保存到json文件中.json文件中保存数据的格式应如下所示.[{"title":"some text","description":"some text","info":"some text","username&q…

java之包装类与BigInteger、BigDecimal

一、包装类 &#xff08;1&#xff09;包装类与原类型 Integer int 的包装类 Boolean boolean 的包装类 Character char的包装类 Double double的包装类 Float float 的包装类 Byte …

哈希值 哈希表_哈希杰森

哈希值 哈希表我最近写了一个简单的库&#xff0c;可预测地对json进行哈希处理 。 该实用程序基于出色的Jackson Json解析库构建 问题 我需要从相当大的基于json的内容生成的哈希值&#xff0c;以便稍后确定该内容是否发生了更改。 将json视为字符串不是格式化的选项&#xf…