JAVA - Comparable接口 与 Comparator接口
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接口的更多相关文章
- Java中实现对象的比较:Comparable接口和Comparator接口
在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...
- Java:实现对象的比较 comparable接口和comparator接口
在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...
- Java—集合框架 Collections.sort()、Comparable接口和Comparator接口
Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...
- Java6.0中Comparable接口与Comparator接口详解
Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...
- java中Comparatable接口和Comparator接口的区别
1.不同类型的排序规则 .自然排序是什么? 自然排序是一种升序排序.对于不同的数据类型,升序规则不一样: BigDecimal BigInteger Byte Double Float Int ...
- Comparable接口与Comparator接口的比较————总结
之前的两篇文章主要学习了Comparable接口和Comparator接口的学习.既然已经学习完了,现在就趁热打铁,进行总结吧! Comparable接口和Comparator接口的共同点: 1. 都 ...
- 比较器:Compare接口与Comparator接口区别与理解
一.实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用 java.util.Arrays.sort(对象数组名称),可以自定义排序规则. 不同之处: ...
- Comparatable接口和Comparator接口的使用与区别
这篇博文可以为你解决的问题如下: 什么是自然排序 Collections.sort()与Arrays.sort()的异同点 Comparatable接口和Comparator接口各自的排序依据(理论讲 ...
- Java中的Comparable接口和Comparator接口
Comparator位于包java.util下,比较器,是在集合外部定义排序.Comparable位于包java.lang下,代表当前对象可比较的,是在集合内部实现排序. Comparable代表一个 ...
- Java之Comparable接口和Comparator接口
Comparable & Comparator 都是用来实现集合中元素的比较.排序的: Comparable 是在集合内部定义的方法实现的排序: Comparator 是在集合外部实现的排序: ...
随机推荐
- java常识和好玩的注释
如字符串使用strXXXboolean使用isXXX,hasXXX Vector vProducts= new Vector(); Array aryUsers= new Array(); 类与接口基 ...
- statistic学习笔记
1. 假设检验:就是对于符合一定前提条件的数据,先作一个假设H0,还有一个备择假设H1(一般是H0的反面,或者是H0不包含的情况),通过一定的计算公式,算出一个值(比如开方检验就是开方值),这个值的理 ...
- JQuery相关的网络资源
jquery插件列表 国外网站:http://plugins.jquery.com/ 国内网站:http://www.oschina.net/project/tag/273/jquery
- 突然间,对JAVA也找到点感觉了。
书上没有那段代码,我自己修修补补弄完全了呢.... 就是感觉体系有点宏大,不要急,慢慢玩~!~~ 这个世界很精彩哟~~: QuizCard.java package QuizCard.sky.com; ...
- ShapeDrawable 资源
ShapeDrawable 用于定义一个基本的几何图形(如矩形.圆形.线条等),定义 ShapeDrawable 的 XML 文件的根元素是<shape.../>元素,该元素可指定如下属性 ...
- [Android] hid设备按键流程简述
hexdump /dev/hidraw0就能看到usbhid设备传输过来的裸流 如:按下Input键 003ae60 0000 0096 8000 006b 0000 0000 0000 0000 * ...
- POJ 1637 混合图欧拉回路
先来复习一下混合图欧拉回路:给定一张含有单向边和双向边的图,使得每一点的入度出度相同. 首先对于有向边来说,它能贡献的入度出度是确定的,我们不予考虑.对于无向图,它可以通过改变方向来改变两端点的出入度 ...
- Hadoop CombineFileInputFormat实现原理及源码分析
Hadoop适用于少量的大文件场景,而不是大量的小文件场景(这里的小文件通常指文件大小显著小于HDFS Block Size的文件),其主要原因是因为FileInputFormat在为这些小文件生成切 ...
- delphi 自定义消息
delphi 自定义消息 消息描述 Tmsg是 Windows系统用来记录描述一个具体的windows消息的.就是windows 用于封装应用程序及系统程序发生的消息,它是操作系统使用 ...
- HDOJ(HDU) 2097 Sky数(进制)
Problem Description Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=2 ...