比较器: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 ...
随机推荐
- kvc(键-值编码)
kvc(键-值编码) { NSString *_name; Author *_author; NSArray *_kvcArray; float price;} //kvc,setValue 的设的值 ...
- FLEX各种特效集合
http://www.noupe.com/adobe/flex-developers-toolbox-free-components-themes-and-tutorials.html经典中的经典 h ...
- python 之sqlalchemy many to many
# -*- coding: utf-8 -*- """ @author: zengchunyun """ from sqlalchemy i ...
- java分享第十天(http协议简介)
URL模式URL(Uniform Resource Locator) 地址用于描述一个网络上的资源, 基本格式如下scheme 指定低层使用的协议(例如:http, https, ftp)host H ...
- Java WebService 简单实例
前言:朋友们开始以下教程前,请先看第五大点的注意事项,以避免不必要的重复操作. 一.准备工作(以下为本实例使用工具) 1.MyEclipse10.7.1 2.JDK 1.6.0_22 二.创建服务端 ...
- python与shell的3种交互方式介绍
[目录] 1.os.system(cmd) 2.os.popen(cmd) 3.利用subprocess模块 4.subprocessor模块进阶 [概述] 考虑这样一个问题,有hello.py脚本, ...
- SqlServer 笔记一 某表中每个月的产品数量(DATENAME() 与 DATEPART()、YEAR())
1.使用 DATENAME() 函数 SELECT DATENAME(yyyy, [columnName]) + '/' + DATENAME(mm, [columnName]) AS monthDa ...
- awk之特征相同行的合并 ~转
awk之特征相同行的合并 文本: 1001 hisk01 1001 hisk02 1001 hisk03 1002 hisk04 1002 hisk05 1002 hisk06 1003 ...
- apachetop 实时监控apache指定日志
编译安装,压缩包戳我下载 帮助文档 # apachetop -h ApacheTop v0.12.6 - Usage: File options: -f logfile open logfile (a ...
- Maven的依赖和传递性质
1. 引入项目所需jar包 Maven项目直白的一大特点就是一般情况下不需要去自行下载jar包以及目标jar包的依赖包并导入,只需要在去Maven的中央仓库http://mvnrepository.c ...