比较器:Compare接口与Comparator接口区别与理解
一、实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用
java.util.Arrays.sort(对象数组名称),可以自定义排序规则。
不同之处:
1 排序规则实现的方法不同
Comparable接口的方法:compareTo(Object o)
Comparator接口的方法:compare(T o1, To2)
2 类设计前后不同
Comparable接口用于在类的设计中使用;
Comparator接口用于类设计已经完成,还想排序(Arrays);
二、Comparable接口的实例操作
Student类创建时实现Comparable接口,覆写compareTo()方法,
成绩按从高到低排序,成绩相等按年龄从小到大排序。
package ch11.lei.ji;
/*实现Comparator接口的类可以方便的排序,
* 覆写compareTo接口
* java.util.Arrays.sort(对象类数组),*/
class Student implements Comparable<Student> { // 指定类型为Student
private String name ;
private int age ;
private float score ;
public Student(String name,int age,float score){
this.name = name ;
this.age = age ;
this.score = score ;
}
public String toString(){
return name + "\t\t" + this.age + "\t\t" + this.score ;
}
public int compareTo(Student stu){ // 覆写compareTo()方法,实现排序规则的应用
if(this.score>stu.score){
return -1 ;
}else if(this.score<stu.score){
return 1 ;
}else{
if(this.age>stu.age){
return 1 ;
}else if(this.age<stu.age){
return -1 ;
}else{
return 0 ;
}
}
}
};
public class Comparable01{
public static void main(String args[]){
Student stu[] = {new Student("张三",20,90.0f),
new Student("李四",22,90.0f),new Student("王五",20,99.0f),
new Student("赵六",20,70.0f),new Student("孙七",22,100.0f)} ;
java.util.Arrays.sort(stu) ; // 进行排序操作
for(int i=0;i<stu.length;i++){ // 循环输出数组中的内容
System.out.println(stu[i]) ;
}
}
};
三、Comparator接口实例操作
Student01类原先没有比较器,类完成后构建一个比较器StudentComparator类
按年龄从大到小排序。
package ch11.lei.ji;
import java.util.* ;
class Student01{ // 指定类型为Student
private String name ;
private int age ;
public Student01(String name,int age){
this.name = name ;
this.age = age ;
}
public boolean equals(Object obj){ // 覆写equals方法
if(this==obj){
return true ;
}
if(!(obj instanceof Student)){
return false ;
}
Student01 stu = (Student01) obj ;
if(stu.name.equals(this.name)&&stu.age==this.age){
return true ;
}else{
return false ;
}
}
public void setName(String name){
this.name = name ;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
public String toString(){
return name + "\t\t" + this.age ;
}
}; class StudentComparator implements Comparator<Student01>{ // 实现比较器
// 因为Object类中本身已经有了equals()方法
public int compare(Student01 s1,Student01 s2){
if(s1.equals(s2)){
return 0 ;
}else if(s1.getAge()<s2.getAge()){ // 按年龄比较
return 1 ;
}else{
return -1 ;
}
}
}; public class Comparator01{
public static void main(String args[]){
Student01 stu[] = {new Student01("张三",20),
new Student01("李四",22),new Student01("王五",20),
new Student01("赵六",20),new Student01("孙七",22)} ;
java.util.Arrays.sort(stu,new StudentComparator()) ; // 进行排序操作
for(int i=0;i<stu.length;i++){ // 循环输出数组中的内容
System.out.println(stu[i]) ;
}
}
};
比较器:Compare接口与Comparator接口区别与理解的更多相关文章
- java中Comparatable接口和Comparator接口的区别
1.不同类型的排序规则 .自然排序是什么? 自然排序是一种升序排序.对于不同的数据类型,升序规则不一样: BigDecimal BigInteger Byte Double Float Int ...
- Comparatable接口和Comparator接口的使用与区别
这篇博文可以为你解决的问题如下: 什么是自然排序 Collections.sort()与Arrays.sort()的异同点 Comparatable接口和Comparator接口各自的排序依据(理论讲 ...
- Java中实现对象的比较:Comparable接口和Comparator接口
在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...
- Java:实现对象的比较 comparable接口和comparator接口
在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...
- Java—集合框架 Collections.sort()、Comparable接口和Comparator接口
Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...
- Comparable接口与Comparator接口的比较————总结
之前的两篇文章主要学习了Comparable接口和Comparator接口的学习.既然已经学习完了,现在就趁热打铁,进行总结吧! Comparable接口和Comparator接口的共同点: 1. 都 ...
- Java6.0中Comparable接口与Comparator接口详解
Java6.0中Comparable接口与Comparator接口详解 说到现在,读者应该对Comparable接口有了大概的了解,但是为什么又要有一个Comparator接口呢?难道Java的开发者 ...
- Comparable接口与Comparator接口的区别
1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Pe ...
- comparable接口 和 comparator接口的特点与区别
1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的. 什么是自定义class: 如 public class Pe ...
随机推荐
- 我的jQuery源码读后感
(function(window, undefined) { // 构造jQuery对象 var jQuery = (function() { var jQuery = function(select ...
- Compass的误解与新发现
最后个人感觉 Compass是 css世界的.NET 很久以前看到 Compass 误以为是css编译器,因为总是在看到如何安装Scss的文章里面看到的 知道最近不小心进入 Compass官网溜达,才 ...
- NOI2015 题解
[NOI2015]程序自动分析 离散化+并查集. [NOI2015]软件包管理器 [Noi2015]寿司晚宴 [Noi2015]荷马史诗 [NOI2015]品酒大会 [Noi2015]小园丁与老司机
- 【Redis】简介与安装
Linux 安装 [root@redis ~]# wget http://download.redis.io/releases/redis-2.8.19.tar.gz 解压缩redis[root@ha ...
- javascript学习之通过class获取元素
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 自动爬取ZiMuZu的内容发布到Wordpress
先说一下大致的步骤. 首先需要模拟浏览器登录网站才能看到相应电影信息, 然后通过正则表达式从网页源代码中筛选出所需要的电影, 最后通过python-wordpress-xmlrpc将信息逐条发布到Wo ...
- 1JavaScript简介
文档对象模型(DOM,Document Object Model)是针对XML但经过扩展用于HTML的应用程序编程接口(API,Application Programming Interface). ...
- Java-马士兵动态代理模式
Java-马士兵动态代理模式 模拟jdk的动态代理的实现原理, 这些东西没有必要写出来,写项目的时候一般用不上,主要是为了面试和理解原理: java动态代理有什么作用 作用非常大,在很多底层框架中都会 ...
- 月四 周2 ii
今日通过色鸟鸟 以此纪念 不是很想玩红鸟鸟 简单来说因为红鸟鸟结束后,五彩斑斓系列就和我没关系了= = 珍惜为每一部作品感伤的每一刻吧
- 包管理工具Carthage使用
iOS项目中第三方开源库的工具有Cocoapods和Carthage,swift官方出了一个包管理工具SPM(Swift Package Manager). 首先,大体讲一下Cocoapods和Car ...