compare() 和 compareTo() 有什么区别? [英] What is the difference between compare() and compareTo()?

查看:28
本文介绍了compare() 和 compareTo() 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 的 compare()compareTo() 方法有什么区别?这些方法给出相同的答案吗?

What is the difference between Java's compare() and compareTo() methods? Do those methods give same answer?

推荐答案

来自 JavaNotes:

  • a.compareTo(b):
    Comparable 接口: 比较值并返回一个 int 值,该 int 值表明这些值比较小于、等于还是大于.
    如果您的类对象具有自然顺序,请实现Comparable 接口并定义此方法.所有具有自然排序的 Java 类都实现 Comparable - 示例:String, 包装类BigInteger

  • a.compareTo(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 接口: 比较两个对象的值.这是作为 Comparator 接口的一部分实现的,典型用途是定义一个或多个实现这一点的小型实用程序类,以传递给诸如 sort 之类的方法() 或用于对 TreeMapTreeSet 等数据结构进行排序.您可能希望为以下内容创建一个 Comparator 对象:

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:

  • 多重比较.提供几种不同的排序方式.例如,您可能希望按姓名、ID、年龄、身高等对 Person 类进行排序……您可以为每个类定义一个 Comparator 以传递给 sort() 方法.莉>
  • 系统类 为您无法控制的类提供比较方法.例如,您可以为按长度比较字符串的字符串定义一个比较器.
  • 策略模式 实现策略模式,在这种情况下,您希望将算法表示为一个对象,您可以将其作为参数传递、保存在数据结构中等.
  • Multiple comparisons. 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.

如果您的类对象具有一种自然排序顺序,则您可能不需要 compare().

If your class objects have one natural sorting order, you may not need compare().

摘自 http://www.digizol.com/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.

用例上下文:

可比较的界面

equals 方法和 ==!= operators 测试相等/不相等,但 不提供方法测试相对值.
某些类(例如,String 和其他具有自然顺序的类)实现了 Comparable 接口,该接口定义了 compareTo() 方法.
如果要将 Collections.sort()Arrays.sort()Collections.sort() 一起使用,则需要在类中实现 Comparable> 方法.

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 Collections.sort() or Arrays.sort() methods.

定义 Comparator 对象

您可以创建比较器来对任何类进行任意排序.
例如,String 类定义了 CASE_INSENSITIVE_ORDER 比较器.

You can create Comparators to sort any arbitrary way for any class.
For example, the String class defines the CASE_INSENSITIVE_ORDER comparator.

这两种方法之间的区别可以与以下概念联系起来:
有序集合:

The difference between the two approaches can be linked to the notion of:
Ordered Collection:

当一个集合被排序时,这意味着你可以以特定(非随机)的顺序(一个 Hashtable 没有被排序)在集合中迭代.

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).

另一个区别,由 HaveAGuess评论:

Another difference, pointed out by HaveAGuess in the comments:

  • Comparable 在实现中并且在界面中不可见,所以当你排序时你并不真正知道会发生什么.
  • Comparator 让您放心,排序将得到很好的定义.
  • 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.

这篇关于compare() 和 compareTo() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆