Comparator与Comparable的区别

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.

Comparable接口会给每个实现了该接口的类的对象指定一个顺序,这个顺序是类的自然顺序。一个List中的所有对象都实现了这个接口,那么可以通过Collection.sort()方法对它们进行排序。实现了该接口的对象可以作为sorted map的key或者sorted set的元素,不需要再指定一个comparator。

它只有一个方法:int compareTo(T o)

---------------------------------------------------------------------------------------

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.

Comparator接口给对象的集合提供一个比较的功能。比较器能够被传递到一个排序的方法(比如:Collections.sort或者Arrays.sort)中来精确控制排序的顺序。

它有两个方法:

int compare(T o1, T o2)

boolean equals(Object obj)

总结一下:

  1. 实现了Comparable接口的对象自身就具备比较功能,而实现了Comparator接口的对象是一个比较器,也就是一个工具,它用于比较那些需要比较的对象。
  2. 利用Collections.sort(List<T> list)方法进行排序的对象必须实现Comparable接口
  3. 可以利用Collections.sort(List<T> list, Comparator<? super T> c)方法进行排序,排序规则自己指定
时间: 2024-07-29 21:59:38

Comparator与Comparable的区别的相关文章

接口Comparator和Comparable的区别和联系

1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的. 什么是自定义class: 如 public class Person{ String name; int age; } 当我们有这么一个personList,里面包含了person1, person2, persion3....., 我们用Collections.sort( personList ), 是得不到预期的结果的. 这时肯定有人要问, 那为什么可以

java中Comparator 和 Comparable的区别

1.Comparable的代码如下: public interface Comparable<T> { public int compareTo(T o); } 2.Comparator的代码如下 public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); // jdk1.8 后的方法 default Comparator<T> reversed() { re

集合排序: Comparator和Comparable的使用区别

Comparator接口 Comparable接口 区别 在Java中使用集合来存储数据时非常常见的,集合排序功能也是常用功能之一.下面看一下如何进行集合排序,常用的方法有: Comparator和Comparable Comparator接口 使用步骤: 新建比较类, 实现Comparator接口, 重写compare方法, package sort; import java.util.Comparator; public class LuckBoyCompare implements Com

java的Comparator和Comparable

java的Comparator和Comparable 当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序.      一.Comparator 强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort. 接口方法:   /**    * @return o1小于.等于或大于o2,分别返回负整数.零或正整数.    */  

[转载] Comparator 与 Comparable

转载自 http://www.cnblogs.com/sunflower627/p/3158042.html 1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Person{ String name; int age }. 当我们有这么一个personList,里面包含了person1, person2, persion3....., 我们用Colle

Comparator和Comparable在排序中的应用

当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序. 一.Comparator 强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort. 接口方法: public class SalesItemComparatorByFirstLetter implements Comparator<SalesItem> { @Over

Comparator与Comparable的应用

当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序. 阅读过程中有任何问题,请联系egg: 邮箱:[email protected]   微博:http://weibo.com/xtfggef 如有转载,请说明出处:http://blog.csdn.net/zhangerqing 一.Comparator 强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collectio

Comparator和Comparable的联系与区别

1.知识点了解 Comparator和Comparable都是用用来实现集合中元素的比较.排序的,所以,经常在集合外定义Comparator接口的方法和集合内实现Comparable接口的方法中实现排序 相同点: 二者的比较方法Comparable的compareTo和compare返回的结果有三种负数.零.正数,分别表示的关系为小于.等于.大于 不同点: Comparator位于java.util包下,属于Collection的一员:Comparable位于java.lang包下 Compar

Java://Comparator、Comparable的用法(按照要求将set集合的数据进行排序输出):

import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; //comparator.comparable的用法(按照要求将map集合的键值对进行顺序输出) import java.util.List; public class Test { public static <T> void sop(T t) { System.out.println(t); } public stat