compare()和compareTo()之间有什么区别?(What is the difference between compare() and compareTo()?)
Java的compare()和compareTo()方法有什么区别? 那些方法是否给出相同的答案?
What is the difference between Java's compare() and compareTo() methods? Do those methods give same answer?
最满意答案
来自Javaotes :
(b) : 可比较的接口:比较值并返回一个int,它告诉值比较小于,等于或大于。 如果您的类对象具有自然顺序 ,请执行Comparable<T>接口并定义此方法。 具有自然排序功能的所有Java类可Comparable<T> - 示例: String , 包装类 , BigInteger
compare(a, b) : 比较器接口:比较两个对象的值。 这是作为Comparator<T>接口的一部分实现的, 典型的用法是定义一个或多个实现此实现的小型实用程序类,以传递诸如sort()方法,或通过排序数据结构(如TreeMap和TreeSet 。 您可能需要为以下内容创建一个Comparator对象:
多重比较 。 提供几种不同的排序方法。 例如,您可能希望通过名称,ID,年龄,高度等对Person类进行排序。您可以为每个类定义要传递给sort()方法的Comparator。 系统类为不能控制的类提供比较方法。 例如,您可以定义一个比较器,用于比较它们的长度。 策略模式为了实现一个策略模式,这是一种将算法表示为可以作为参数传递的对象的情况,可以保存在数据结构等中。如果你的类对象有一个自然的排序顺序,你可能不需要compare()。
Digizol总结
可比 可比较的对象能够与另一个对象进行比较。
比较 比较器对象能够比较两个不同的对象。 该类没有比较其实例,而是一些其他类的实例。
用例上下文:
可比较的界面
equals方法和==和!= 运算符测试相等/不等式,但不提供测试相对值的方法 。 一些类(例如,String和具有自然排序的其他类)实现了Comparable<T>接口,它定义了一个compareTo()方法。 如果要将其与Collecti.sort()或Arrays.sort()方法一起使用,您将需要在类中实现Comparable<T> 。
定义比较器对象
您可以创建比较器来对任何类进行排序 。 例如, String类定义了CASE_ISESITIVE_ORDER比较器 。
这两种方法之间的区别可以与以下概念相关: 有序集合
当订购一个集合时,这意味着您可以以特定(非随机)顺序对集合进行迭代(不Hashtable )。
具有自然顺序的集合不仅仅是订购,而是排序 。 定义自然秩序可能很困难! (如在自然字符串顺序 )。
另一个区别,由HaveAGuess在评论中指出:
Comparable是在实现中,不可见的界面,所以当你排序你不知道会发生什么。 Comparator让您放心,订购将被很好地定义。From Javaotes:
(b): Comparable interface : Compares values and returns an int which tells if the values compare less than, equal, or greater than. If your class objects have a natural order, implement the Comparable<T> interface and define this method. All Java classes that have a natural ordering implement Comparable<T> - Example: String, wrapper classes, BigInteger
compare(a, b): Comparator interface : Compares values of two objects. This is implemented as part of the Comparator<T> interface, and the typical use is to define one or more small utility classes that implement this, to pass to methods such as sort() or for use by sorting data structures such as TreeMap and TreeSet. You might want to create a Comparator object for the following:
Multiple comparis. To provide several different ways to sort something. For example, you might want to sort a Person class by name, ID, age, height, ... You would define a Comparator for each of these to pass to the sort() method. System class To provide comparison methods for classes that you have no control over. For example, you could define a Comparator for Strings that compared them by length. Strategy pattern To implement a Strategy pattern, which is a situation where you want to represent an algorithm as an object that you can pass as a parameter, save in a data structure, etc.If your class objects have one natural sorting order, you may not need compare().
Summary from http://www./2008/07/java-sorting-comparator-vs-comparable.html
Comparable A comparable object is capable of comparing itself with another object.
Comparator A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances.
Use case contexts:
Comparable interface
The equals method and == and != operators test for equality/inequality, but do not provide a way to test for relative values. Some classes (eg, String and other classes with a natural ordering) implement the Comparable<T> interface, which defines a compareTo() method. You will want to implement Comparable<T> in your class if you want to use it with Collecti.sort() or Arrays.sort() methods.
Defining a Comparator object
You can create Comparators to sort any arbitrary way for any class. For example, the String class defines the CASE_ISESITIVE_ORDER comparator.
The difference between the two approaches can be linked to the notion of: Ordered Collection:
When a Collection is ordered, it means you can iterate in the collection in a specific (not-random) order (a Hashtable is not ordered).
A Collection with a natural order is not just ordered, but sorted. Defining a natural order can be difficult! (as in natural String order).
Another difference, pointed out by HaveAGuess in the comments:
Comparable is in the implementation and not visible from the interface, so when you sort you don't really know what is going to happen. Comparator gives you reassurance that the ordering will be well defined.#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上一篇:计算机开机后关不了机,电脑关不了机或关机后又重启怎么样解决
下一篇:flask
推荐阅读
留言与评论(共有 17 条评论) |
本站网友 辽宁省西丰县 | 30分钟前 发表 |
系统类为不能控制的类提供比较方法 | |
本站网友 南京最好的男科医院 | 22分钟前 发表 |
Compares values and returns an int which tells if the values compare less than | |
本站网友 山东航空公司官网订票 | 12分钟前 发表 |
or greater than. If your class objects have a natural order | |
本站网友 全聚德烤鸭店 | 17分钟前 发表 |
等于或大于 | |
本站网友 南昌市政公用集团 | 3分钟前 发表 |
the String class defines the CASE_ISESITIVE_ORDER comparator. The difference between the two approaches can be linked to the notion of | |
本站网友 fuckyou | 14分钟前 发表 |
... You would define a Comparator for each of these to pass to the sort() method. System class To provide comparison methods for classes that you have no control over. For example | |
本站网友 panadol | 16分钟前 发表 |
implement the Comparable<T> interface and define this method. All Java classes that have a natural ordering implement Comparable<T> - Example | |
本站网友 酷能 | 13分钟前 发表 |
... You would define a Comparator for each of these to pass to the sort() method. System class To provide comparison methods for classes that you have no control over. For example | |
本站网友 瘦脸针效果图 | 11分钟前 发表 |
而是排序 | |
本站网友 零陵郡 | 14分钟前 发表 |
所以当你排序你不知道会发生什么 | |
本站网友 三星在西安建厂 | 29分钟前 发表 |
but sorted. Defining a natural order can be difficult! (as in natural String order). Another difference | |
本站网友 许昌奥斯卡 | 21分钟前 发表 |
以传递诸如sort()方法 | |
本站网友 姜堰二手房 | 7分钟前 发表 |
订购将被很好地定义 | |
本站网友 涪陵房价 | 26分钟前 发表 |
您将需要在类中实现Comparable<T> | |
本站网友 厌氧菌感染 | 16分钟前 发表 |
这是一种将算法表示为可以作为参数传递的对象的情况 | |
本站网友 南药王牙膏 | 27分钟前 发表 |
该类没有比较其实例 |