java 接口Comparable和Comparator的使用

一、Comparable接口的使用

     需比较的对象实现Comparable接口,重写public int compareTo(Object other){} 即可

import java.util.ArrayList;

import java.util.Collections;

public class TestComparable{
    public static void main(String[] args){
        ArrayList<Student> students = new ArrayList<Student>();
        students.add(new Student(4));
        students.add(new Student(2));
        students.add(new Student(1));
        students.add(new Student(9));
        
        Collections.sort(students);  //按照我们重写的规定排序
        
        System.out.println(students.toString());
        
        for(Student student:students){
            System.out.println(student);
        }
    }
}

//使用泛型

class Student  implements Comparable<Student>{

    private int id;
    public Student(int id){
        this.id = id;
    }
    public int compareTo(Student other){
        //Student o = (Student)other;
        if(this.id>other.id){
            return 1;
        }else if(this.id<other.id){
            return -1;
        }else{
            return 0;
        }
    }

    public String toString(){
        return "student id = " + id;
    }
}

 

//未使用泛型

/*

class Student  implements Comparable{

    private int id;
    public Student(int id){
        this.id = id;
    }
    public int compareTo(Object other){//形参必须为Object
        Student o = (Student)other;
        if(this.id>o.id){   //当前对象与形参对象比较,大于零就是升序,小于零降序
            return 1;
        }else if(this.id<o.id){
            return -1;
        }else{
            return 0;
        }
    }

    public String toString(){
        return "student id = " + id;
    }

}

*/

输出结果:

[student id = 1, student id = 2, student id = 4, student id = 9]
student id = 1
student id = 2
student id = 4
student id = 9

 

二、Comparator接口的使用

 需比较的对象实现Comparator接口,重写public int compare(Object o1,Object o2){} 即可

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class TestComparator {
    public static void main(String[] args){
        ArrayList<Emp> emps = new ArrayList<Emp>();
        emps.add(new Emp(5));
        emps.add(new Emp(1));
        emps.add(new Emp(78));
        emps.add(new Emp(3));
        emps.add(new Emp(13));
        
        Collections.sort(emps,new Emp()); //按照我们重写的规定排序
        
        System.out.println(emps.toString());
        
        for(Emp emp:emps){
            System.out.println(emp);
        }    
    }
    
}

class Emp implements Comparator{
    private int id;
    public Emp(int id){
        this.id = id;
    }
    public Emp(){
        
    }
    public int compare(Object o1,Object o2){
        Emp emp1 = (Emp)o1;
        Emp emp2 = (Emp)o2;
        if(emp1.id>emp2.id){//第一个对象的成员大于第二个返回1时升序
            return 1;
        }else if(emp1.id<emp2.id){
            return -1;
        }else{
            return 0;
        }
    }
    
    public String toString(){
        return "emp id = " + this.id;
    }
}

输出结果:

[emp id = 1, emp id = 3, emp id = 5, emp id = 13, emp id = 78]
emp id = 1
emp id = 3
emp id = 5
emp id = 13
emp id = 78

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值