一、实现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接口区别与理解的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  8. Comparable接口与Comparator接口的区别

    1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Pe ...

  9. comparable接口 和 comparator接口的特点与区别

    1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的. 什么是自定义class: 如 public class Pe ...

随机推荐

  1. Leetcode 4Sum

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  2. EasyUI 页面分页

    DAO package com.hanqi.dao; import java.util.ArrayList; import java.util.List; import org.hibernate.S ...

  3. VMware与virtualbox安装centos7连接网络不可达问题解决笔记(连接网络)

    我最初是安装vmware遇到访问不到网络,按网上的配置方法都不能解决.然后我感觉可能跟系统有关,我装的是centos,然后我试着在virtualbox上安装看遇到什么问题. 用virtualbox安装 ...

  4. hibernate_Restrictions用法

    方法   说明 Restrictions.eq = Restrictions.allEq 利用Map来进行多个等于的限制 Restrictions.gt > Restrictions.ge &g ...

  5. DOS 命令For精解示例

    最基本形态: 在cmd 窗口中:for %I in (command1) do command2 在批处理文件中:for %%I in (command1) do command2 在批处理中,FOR ...

  6. 一个人java深入理解java logback配置

    http://blog.csdn.net/initphp/article/category/1230072/2

  7. Apache Storm源码阅读笔记

    欢迎转载,转载请注明出处. 楔子 自从建了Spark交流的QQ群之后,热情加入的同学不少,大家不仅对Spark很热衷对于Storm也是充满好奇.大家都提到一个问题就是有关storm内部实现机理的资料比 ...

  8. rabbitmq使用心得

    因为公司项目需要使用消息中间件,实现相关业务的异步处理,所有选用了rabbitmq.通过看文档,爬过一个一个坑,终于还是实现了相关功能. 直接上配置文件: <?xml version=" ...

  9. socket - socketserver - start TCP server

    前面提到如何使用socket模块启动tcpserver: 创建socket:sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 绑定ip: ...

  10. linux建立文件夹软连接

    linux建立文件夹软连接,并强制覆盖 ln -sfn /home/var/log/httpd/logs logs 这将在当前目录下建立logs软连接,指向/home/var/log/httpd/lo ...