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 是在集合外部实现的排序: ...
随机推荐
- ReetrantLock Synchronized Atomic的性能对比
之前看到了一篇帖子关于Lock和Synchronized的性能,写的是Lock比Synchronized的性能要好,可是,我试了下,结果却不是这样的,我所使用的JDK的版本是1.7,可能跟原帖作者用的 ...
- A simple stack
// simple stack.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include<iostream>using na ...
- c#结构体和字节数组的转换、字节数组和stream的转换
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/streambytsstruct.html using System; using ...
- [LeetCode OJ] Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Java学习----你可以告诉对象该怎么做(方法中传参)
对象根据参数传递来的条件执行相应的功能. package org.demo.app2; public class App2 { public void print(String msg, int nu ...
- Tomcat安装阿里云免费证书
安装证书 Tomcat支持JKS格式证书,从Tomcat7开始也支持PFX格式证书,两种证书格式任选其一.下载包中包含PFX格式证书和密码文件. 1.PFX证书安装 找到安装 Tomcat 目录下该文 ...
- WINFORM的DataGridView使用点滴
1.如果有超过一屏的数据,想通过关键字查找到相关记录,然后再定位之,做法如下:遍历所有行,把某单元格的值和关键字对比,找到后清除所有选择行,然后把当前行设为选择,然后把grid的CurrentCell ...
- php防止sql注入函数
$magic_quotes_gpc = get_magic_quotes_gpc(); @extract(daddslashes($_COOKIE)); @extract(daddslashes($_ ...
- Codeforces 556A Case of the Zeros and Ones(消除01)
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description Andr ...