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

查看:219
本文介绍了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)

    可比较的界面:比较值和返回一个int,告诉我们比较的值是小于,等于还是大于。

    如果您的类对象具有自然顺序,请实现 Comparable< ; T> 接口并定义此方法。所有具有自然排序实现的Java类 Comparable< T> - 示例: 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

比较(a,b)

比较器界面:比较两个对象的值。这是作为 Comparator< T> 接口的一部分实现的,而的典型用途是定义一个或多个实现此功能的小实用程序类,以传递给方法例如 sort()或者用于排序数据结构,例如 TreeMap TreeSet 即可。您可能希望为以下内容创建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方法和 == != 运算符测试相等/不等,但不提供测试相对值的方法

某些类(例如String)和其他具有自然排序的类)实现 Comparable< T> 接口,该接口定义 compareTo()方法。

如果你想在 Collections.sort()中使用它,你会希望在你的类中实现 Comparable< T> Arrays.sort()方法。

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 class定义 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 评论


  • 可比较在实现中,并且从界面中看不到,所以当你排序时,你真的不知道会发生什么。

  • 比较器让您放心,订购将得到很好的定义。

  • 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天全站免登陆