Similarities:
Both are custom ways to compare two objects.
Both return an int describing the relationship between two objects.

Differences:

  • Comparator

compare() is from the Comparator interface.

A comparator object is capable of comparing two different objects. The class is not comparing its instances, but some other class’s instances.

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 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.
Comparator comp =newMyComparator();
int result = comp.compare(object1, object2);
  • Comparable 

compareTo() is from the Comparable interface.

A comparable object is capable of comparing itself with another object. It allows an object to be compared to objects of similar type.

If your class objects have a natural order, implement the Comparable interface and define this method. All Java classes that have a natural ordering implement this (String, Double, BigInteger, ...).

String s ="hi";
int result = s.compareTo("bye");

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

 
 
 
 

JAVA - Comparable接口 与 Comparator接口的更多相关文章

  1. Java中实现对象的比较:Comparable接口和Comparator接口

    在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...

  2. Java:实现对象的比较 comparable接口和comparator接口

    在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...

  3. Java—集合框架 Collections.sort()、Comparable接口和Comparator接口

    Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...

  4. Java6.0中Comparable接口与Comparator接口详解

    Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...

  5. java中Comparatable接口和Comparator接口的区别

    1.不同类型的排序规则 .自然排序是什么?   自然排序是一种升序排序.对于不同的数据类型,升序规则不一样:   BigDecimal BigInteger Byte Double Float Int ...

  6. Comparable接口与Comparator接口的比较————总结

    之前的两篇文章主要学习了Comparable接口和Comparator接口的学习.既然已经学习完了,现在就趁热打铁,进行总结吧! Comparable接口和Comparator接口的共同点: 1. 都 ...

  7. 比较器:Compare接口与Comparator接口区别与理解

    一.实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用 java.util.Arrays.sort(对象数组名称),可以自定义排序规则. 不同之处: ...

  8. Comparatable接口和Comparator接口的使用与区别

    这篇博文可以为你解决的问题如下: 什么是自然排序 Collections.sort()与Arrays.sort()的异同点 Comparatable接口和Comparator接口各自的排序依据(理论讲 ...

  9. Java中的Comparable接口和Comparator接口

    Comparator位于包java.util下,比较器,是在集合外部定义排序.Comparable位于包java.lang下,代表当前对象可比较的,是在集合内部实现排序. Comparable代表一个 ...

  10. Java之Comparable接口和Comparator接口

    Comparable & Comparator 都是用来实现集合中元素的比较.排序的: Comparable 是在集合内部定义的方法实现的排序: Comparator 是在集合外部实现的排序: ...

随机推荐

  1. NHibernate考察实例:简单映射

    建立一个Company类用来测试,对应的表为TBLCOMPANY.   1. 下载NHibernate(版本1.2.0.CR1),将NHibernate \bin\net-2.0下面的文件拷贝到lib ...

  2. 如何根据Jquery实现两级联动

    <script language="javascript" type="text/javascript" > $(function (){      ...

  3. javescript扩展方法

    <script type="text/javascript"> //扩展方法 '原型'->'prototype' //通过类对像的prototype设置扩展方法 ...

  4. HTML5手机开发——滚动和惯性缓动

    1. 滚动 以下是三种实现方式: 1) 利用原生的css属性 overflow: scroll div id= parent style = overflow:scroll; divid='conte ...

  5. [Struts2学习笔记] -- 环境配置

    在创建好WebProject后,就可以开始进行Struts2的环境配置,可以到Struts2官网下载,本环境使用struts-2.3.24.1版本. 首先导入必要的jar包到WebProject的/W ...

  6. C语言初学 数组 打印菱形

    #include<stdio.h> #include<stdlib.h> int main() { int n,i,j; printf("---开始打印符号--\n& ...

  7. 关于几种常用的Adapter使用区别

    Adapter常用的实现类如下: 1.ArrayAdapter:简单.易用的Adapter,通常用于将数组或List集合的多个值包装成多个列表项. 2.SimpleAdapter:并不简单.功能强大的 ...

  8. mysql一个表中多个字段对应另一个表的id如何查询?

    比如有如下2个表 a 和baaID b1ID b2ID b3ID1 1 3 52 2 4 6bbID bCon1 苹果2 香蕉3 国内4 国外5 出口6 进口其中a表中的b1ID,b2ID,b3ID都 ...

  9. XCode7打包上传报错

      在XCode7上传应用时,上传失败遇到两个错误,提示如下: ERROR ITMS-90535: "Unexpected CFBundleExecutable Key. The bundl ...

  10. Heapsort 堆排序算法详解(Java实现)

    Heapsort (堆排序)是最经典的排序算法之一,在google或者百度中搜一下可以搜到很多非常详细的解析.同样好的排序算法还有quicksort(快速排序)和merge sort(归并排序),选择 ...