博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java比较器
阅读量:7008 次
发布时间:2019-06-28

本文共 4397 字,大约阅读时间需要 14 分钟。

Comparable接口

Comparable接口实际上属于比较器的操作接口,定义如下:

public interface Comparable<T>{ 

//接口上使用了泛型

 int compareTo(T o); //定义compareTo方法,此方法完成排序

}

compareTo()方法的三种返回值类型:

小于:-1

等于:0

大于:1

1 package com.fwj.compare; 2  3 public class Student implements Comparable
{ 4 5 private String name; 6 private int age; 7 private int score; 8 9 public Student(String name, int age, int score) {10 super();11 this.name = name;12 this.age = age;13 this.score = score;14 }15 16 @Override17 public String toString() {18 return "Student [name=" + name + ", age=" + age + ", score=" + score19 + "]";20 }21 22 @Override23 public int compareTo(Student stu) { //按成绩升序排序,如果成绩相同按年龄升序排序24 25 if(this.score>stu.score){26 return 1;27 }else if(this.score
stu.age){31 return 1;32 }else if(this.age

运行结果:

1 =================排序前=================== 2 Student [name=fwj, age=23, score=90] 3 Student [name=hpf, age=26, score=83] 4 Student [name=wh, age=25, score=83] 5 Student [name=lw, age=20, score=86] 6 =================排序后=================== 7 Student [name=wh, age=25, score=83] 8 Student [name=hpf, age=26, score=83] 9 Student [name=lw, age=20, score=86]10 Student [name=fwj, age=23, score=90]

Comparator接口

如果一个类已经开发完成,则肯定不能修改,所以,此时为了让此类具有排序功能,可以使用comparator完成排序的操作。

1 package com.fwj.compare; 2  3 public class Student { 4  5     private String name; 6     private int age; 7     private int score; 8      9     public String getName() {10         return name;11     }12 13     public void setName(String name) {14         this.name = name;15     }16 17     public int getAge() {18         return age;19     }20 21     public void setAge(int age) {22         this.age = age;23     }24 25     public int getScore() {26         return score;27     }28 29     public void setScore(int score) {30         this.score = score;31     }32 33     public Student(String name, int age, int score) {34         super();35         this.name = name;36         this.age = age;37         this.score = score;38     }39 40     @Override41     public boolean equals(Object obj) {
//复写equals,完成对象比较42 43 if(this==obj){44 return true;45 }46 if(!(obj instanceof Student)){47 return false;48 }49 Student stu = (Student)obj;50 if(this.name==stu.name&&this.age==stu.age&&this.score==stu.score){51 return true;52 }else{53 return false;54 }55 }56 57 @Override58 public String toString() {59 return "Student [name=" + name + ", age=" + age + ", score=" + score60 + "]";61 }62 63 }
1 package com.fwj.compare; 2  3 import java.util.Comparator; 4  5 public class StudentComparetor implements Comparator
{ 6 7 @Override 8 public int compare(Student stu1, Student stu2) { 9 if(stu1.getScore()>stu2.getScore()){10 return -1;11 }else if(stu1.getScore()
stu2.getAge()){15 return -1;16 }else if(stu1.getAge()
1 package com.fwj.compare; 2  3 import java.util.Arrays; 4  5 public class Test { 6  7     public static void main(String[] args) { 8          9         Student stu[] = {
new Student("fwj",23,90),10 new Student("hpf",26,83),11 new Student("wh",25,83),12 new Student("lw",20,86)13 };14 System.out.println("=================排序前===================");15 print(stu);16 System.out.println("=================排序后===================");17 Arrays.sort(stu, new StudentComparetor());18 print(stu);19 }20 public static void print(Student stu[]){21 22 for (Student student : stu) {23 System.out.println(student);24 }25 }26 27 }

 运行结果:

1 =================排序前=================== 2 Student [name=fwj, age=23, score=90] 3 Student [name=hpf, age=26, score=83] 4 Student [name=wh, age=25, score=83] 5 Student [name=lw, age=20, score=86] 6 =================排序后=================== 7 Student [name=fwj, age=23, score=90] 8 Student [name=lw, age=20, score=86] 9 Student [name=hpf, age=26, score=83]10 Student [name=wh, age=25, score=83]

 

转载于:https://www.cnblogs.com/mingluosunshan/p/3229973.html

你可能感兴趣的文章
golang服务端, 游戏公测时遇到的socket写超时的问题, 也是游戏框架的设计问题
查看>>
oracle 定时器
查看>>
mysqld_multi 多实例启动mysql
查看>>
配置linux下的vimrc
查看>>
glusterfs Self-Heal and Re-Balance Operations
查看>>
Python文件夹与文件的操作
查看>>
Apache 启动遇到问题解决
查看>>
final和static
查看>>
win7 64位 下USB转COM驱动安装方法
查看>>
QString 与中文问题
查看>>
MPLS ××× 配置步聚
查看>>
BGP 管理距离修改及分析
查看>>
我的友情链接
查看>>
uptime详解,最通俗的说明了cpu平均负载
查看>>
docker-compose 学习:通过 Dockerfile 和 build 指令搭建 LNMP
查看>>
关于android服务器推送解决方案的学习
查看>>
我的友情链接
查看>>
归档模式和非归档模式 Oracle 10g学习系列(4)
查看>>
多线程程序,只有黑盒测试是不够的!
查看>>
服务器上安装nodejs
查看>>