Java比较器

Java比较器
1.实现Comparable接口
Comparable接口的定义
public interface Comparable<T>
{
    public int compareTo(T  o); 
}


compareTo方法的返回值:
a. 1 :表示前者大于后者
b. -1:表示前者小于后者
c. 0 :表示两者相等
此种返回值,比较器将会实现从小到大的排序。如果想实现从大到小的排序,则可以将a、b两种情况的值设置微信相反。即a:-1,b:1




范例:
package com.zgy.comparator;


public 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 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() //覆写toString()方法
    {
    return name+"\t\t"+age+"\t\t"+score;
    }
    public int compareTo(Student stu) //实现Comparable接口中的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;
    }
    }
    }
}






package com.zgy.comparator;


import java.util.Arrays;


public class CompareStudent 
{


public static void main(String[] args) 
{
Student[] stu = {new Student("张三",20,90.0f),new Student("李四",22,90.0f),
new Student("王五",20,90.0f),new Student("赵六",20,70.0f),new Student("孙七",22,100.0f)};

Arrays.sort(stu); //比较器已经实现,调用Arrays类的sort()方法
for(Student x : stu) //foreach输出
{
System.out.println(x); //用Student类中已经覆写过的toString()方法输出
}
}


}




2.实现Comparator接口
接口定义:
public interface Comparator<T>()
{
    public int compare(T o1,T o2);
boolean equeals(Object obj);
}
此处的compare()方法要有两个参数。
此接口与Comaprable接口的不同点:
需要单独指定好一个比较器的比较规则的类,才可以完成排序。
如果一个类已经开发完成,并且此类建立的初期没有实现Comparable接口,则可以用Comparator接口进行补救




package com.zgy.comparator2;


public class Student 
{
    private String name;
    private int age;
    public Student(String name,int age)
    {
    this.name = name;
    this.age = age;
    }
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 boolean equals(Object obj) //覆写equals()方法
                                                                //如果比较的两个对象是同一个对象则为true
    {
    if(this == obj)
    {
    return true;
    }
    else if(!(obj instanceof Student))
    {
    return false;
    }
    else
    {
    Student stu = (Student)obj;
    if(this.name.equals(stu.name) && this.age == stu.age) //三个属性都相同,则为true
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }
    public String toSting() //覆写toString()方法
    {
    return name+"\t\t"+age+"\t\t";
    }
}






package com.zgy.comparator2;


import java.util.Comparator;


public class StudentComparator implements Comparator<Student>
{
    public int compare(Student stu1,Student stu2) //时间Comparator接口中的compare()方法
    {
    if(stu1.equals(stu2)) //比较的两个对象是同一个对象,返回0
    {
    return 0;
    }
   
    else if(stu1.getAge() > stu2.getAge())                 //年龄从小到大排序
    {
    return 1;
    }
    else if(stu1.getAge() < stu2.getAge())
    {
    return -1;
    }
    else
    {
    return 0;
    }
    }
}






package com.zgy.comparator2;


import java.util.Arrays;
import java.util.Comparator;


import com.zgy.comparator.Student;


public class ComparatorTest
{


public static void main(String[] args) 
{
Student[] stu = {new Student("张三",20),new Student("李四",22),new Student("王五",20)};
Arrays.sort(stu, new StudentComparator());//比较器已经实现,调用Arrays类的sort()方法
for(Student x : stu) //foreach输出
{
System.out.println(x); //用Student类中已经覆写过的toString()方法输出
}

}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值