java比较器Comparable接口与Comaprator接口的深入分析
程序员文章站
2023-12-18 13:09:34
java的比较器有两类,分别是comparable接口和comparator接口。在为对象数组进行排序时,比较器的作用非常明显,首先来讲解comparable接口。让需要进...
java的比较器有两类,分别是comparable接口和comparator接口。
在为对象数组进行排序时,比较器的作用非常明显,首先来讲解comparable接口。
让需要进行排序的对象实现comparable接口,重写其中的compareto(t o)方法,在其中定义排序规则,那么就可以直接调用java.util.arrays.sort()来排序对象数组,实例如下:
class student implements comparable<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"+age+"\t\t"+score;
}
@override
public int compareto(student o) {
// todo auto-generated method stub
if(this.score>o.score)//score是private的,为什么能够直接调用,这是因为在student类内部
return -1;//由高到底排序
else if(this.score<o.score)
return 1;
else{
if(this.age>o.age)
return 1;//由底到高排序
else if(this.age<o.age)
return -1;
else
return 0;
}
}
}
public class comparabledemo01 {
/**
* @param args
*/
public static void main(string[] args) {
// todo auto-generated method stub
student stu[]={new student("zhangsan",20,90.0f),
new student("lisi",22,90.0f),
new student("wangwu",20,99.0f),
new student("sunliu",22,100.0f)};
java.util.arrays.sort(stu);
for(student s:stu)
{
system.out.println(s);
}
}
}
程序运行结果:
sunliu 22 100.0
wangwu 20 99.0
zhangsan 20 90.0
lisi 22 90.0
但是在设计类的时候,往往没有考虑到让类实现comparable接口,那么我们就需要用到另外的一个比较器接口comparator。
从上面的实例我们可以发现,compareto(t o)只有一个参数,而comparator接口中必须要实现的compare(t o1,t o2)就有两个参数。
代码实例:
package edu.sjtu.ist.comutil;
import java.util.comparator;
class 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 getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public int getage() {
return age;
}
public void setage(int age) {
this.age = age;
}
public float getscore() {
return score;
}
public void setscore(float score) {
this.score = score;
}
public string tostring()
{
return name+"\t\t"+age+"\t\t"+score;
}
}
class studentcomparator implements comparator<student>{
@override
public int compare(student o1, student o2) {
// todo auto-generated method stub
if(o1.getscore()>o2.getscore())
return -1;
else if(o1.getscore()<o2.getscore())
return 1;
else{
if(o1.getage()>o2.getage())
return 1;
else if(o1.getage()<o2.getage())
return -1;
else
return 0;
}
}
}
public class comparabledemo02 {
/**
* @param args
*/
public static void main(string[] args) {
// todo auto-generated method stub
student stu[]={new student("zhangsan",20,90.0f),
new student("lisi",22,90.0f),
new student("wangwu",20,99.0f),
new student("sunliu",22,100.0f)};
java.util.arrays.sort(stu,new studentcomparator());
for(student s:stu)
{
system.out.println(s);
}
}
}
在为对象数组进行排序时,比较器的作用非常明显,首先来讲解comparable接口。
让需要进行排序的对象实现comparable接口,重写其中的compareto(t o)方法,在其中定义排序规则,那么就可以直接调用java.util.arrays.sort()来排序对象数组,实例如下:
复制代码 代码如下:
class student implements comparable<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"+age+"\t\t"+score;
}
@override
public int compareto(student o) {
// todo auto-generated method stub
if(this.score>o.score)//score是private的,为什么能够直接调用,这是因为在student类内部
return -1;//由高到底排序
else if(this.score<o.score)
return 1;
else{
if(this.age>o.age)
return 1;//由底到高排序
else if(this.age<o.age)
return -1;
else
return 0;
}
}
}
public class comparabledemo01 {
/**
* @param args
*/
public static void main(string[] args) {
// todo auto-generated method stub
student stu[]={new student("zhangsan",20,90.0f),
new student("lisi",22,90.0f),
new student("wangwu",20,99.0f),
new student("sunliu",22,100.0f)};
java.util.arrays.sort(stu);
for(student s:stu)
{
system.out.println(s);
}
}
}
程序运行结果:
sunliu 22 100.0
wangwu 20 99.0
zhangsan 20 90.0
lisi 22 90.0
但是在设计类的时候,往往没有考虑到让类实现comparable接口,那么我们就需要用到另外的一个比较器接口comparator。
从上面的实例我们可以发现,compareto(t o)只有一个参数,而comparator接口中必须要实现的compare(t o1,t o2)就有两个参数。
代码实例:
复制代码 代码如下:
package edu.sjtu.ist.comutil;
import java.util.comparator;
class 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 getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public int getage() {
return age;
}
public void setage(int age) {
this.age = age;
}
public float getscore() {
return score;
}
public void setscore(float score) {
this.score = score;
}
public string tostring()
{
return name+"\t\t"+age+"\t\t"+score;
}
}
class studentcomparator implements comparator<student>{
@override
public int compare(student o1, student o2) {
// todo auto-generated method stub
if(o1.getscore()>o2.getscore())
return -1;
else if(o1.getscore()<o2.getscore())
return 1;
else{
if(o1.getage()>o2.getage())
return 1;
else if(o1.getage()<o2.getage())
return -1;
else
return 0;
}
}
}
public class comparabledemo02 {
/**
* @param args
*/
public static void main(string[] args) {
// todo auto-generated method stub
student stu[]={new student("zhangsan",20,90.0f),
new student("lisi",22,90.0f),
new student("wangwu",20,99.0f),
new student("sunliu",22,100.0f)};
java.util.arrays.sort(stu,new studentcomparator());
for(student s:stu)
{
system.out.println(s);
}
}
}
推荐阅读
-
java比较器Comparable接口与Comaprator接口的深入分析
-
java比较器Comparable接口与Comaprator接口的深入分析
-
Java中继承thread类与实现Runnable接口的比较
-
Java 两种比较器的区别:Comparable 与 Comparator
-
Java比较器——Comparable接口和Comparator接口
-
java-如何在不使用比较器和比较器接口的情况下对地图进行排序?如何编写自定义排序?
-
深入浅出了解java比较器 区分Comparable与Comparator的差异以及对象数组的排序
-
Java中关于Comparable与Comparator接口的区别详解
-
Java中关于Comparable与Comparator接口的区别详解
-
Java 两种比较器的区别:Comparable 与 Comparator