码迷,mamicode.com
首页 > 其他好文 > 详细

Comparator和Comparable接口

时间:2014-08-04 14:29:47      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:java   comparator compable   

我们先来说说有了这两个接口带给我们带来了什么好处呢?

例如:我们使用集合List ,添加了10万个员工记录,那你如果要取出来的话,那些数据都是没有进行排序的。所以引入了我们今天的主角:Comparator 和 Comparable 接口

这两个接口告诉你类与类之间如何进行比较。因为知道了如何比较他们之间的大小!才能对他们进行排序!!!

怎么使用呢?so easy!

我们先介绍:Comparable接口

接口。

看案例:

代码清单:员工

class Employment implements Comparable<Employment>{
	public String name;
	public int salary;
	public int age;

	public Employment(String name, int salary, int age) {
		super();
		this.name = name;
		this.salary = salary;
		this.age = age;
	}

	@Override
	public String toString() {
		return "Employment [name=" + name + ", salary=" + salary + ", age="
				+ age + "]";
	}

	@Override
	public int compareTo(Employment o) {
		if(this.salary<o.salary){
			return -1;
		}
		if(this.salary>o.salary){
			return 1;
		}
	    //如果相等,就返回0;
		return 0;
	}
	
}
看一下客户端如何比较这个类:

public class Client{
	public static void main(String[] args) {
		Employment e1=new Employment("诸葛", 2000, 20);
		Employment e2=new Employment("司马", 1000, 20);
		Employment e3=new Employment("刘备", 3000, 20);
		
	    List<Employment>list=new ArrayList<Employment>();
	    list.add(e1);
	    list.add(e2);
	    list.add(e3);
	   
	    //因为现在Employment类可以比较了,那么我们就使用Collections中提供的算法将他们排序
	    Collections.sort(list);
		for(Employment e:list){
			System.out.println(e);
		}
    }
}
输出:

Employment [name=司马, salary=1000, age=20]
Employment [name=诸葛, salary=2000, age=20]
Employment [name=刘备, salary=3000, age=20]

接着我们在来看一下Comparator接口,

class EmploymentCoporator implements Comparator<Employment>{
	@Override
	public int compare(Employment o1, Employment o2) {
		if(o1.salary<o2.salary){
			return -1;
		}
		if(o1.salary>o2.salary){
			return 1;
		}
	    //如果相等,就返回0;
		return 0;
	}
}
在看一下我么的员工类:

class Employment {
	public String name;
	public int salary;
	public int age;

	public Employment(String name, int salary, int age) {
		super();
		this.name = name;
		this.salary = salary;
		this.age = age;
	}

	@Override
	public String toString() {
		return "Employment [name=" + name + ", salary=" + salary + ", age="
				+ age + "]";
	}
}
这时候,员工类就不需要在实现Compable接口了

最后是客户端的代码:

public class Client{
	public static void main(String[] args) {
		Employment e1=new Employment("诸葛", 2000, 20);
		Employment e2=new Employment("司马", 1000, 20);
		Employment e3=new Employment("刘备", 3000, 20);
		
	    List<Employment>list=new ArrayList<Employment>();
	    list.add(e1);
	    list.add(e2);
	    list.add(e3);
	    
	    //这时候,因为算法不知道Employment怎么进行比较,所以这时候我们给它一个“指导”,告诉它怎么进行比较两个类的大小
	    Collections.sort(list,new EmploymentCoporator());
		for(Employment e:list){
			System.out.println(e);
		}
    }
}
输出:

Employment [name=司马, salary=1000, age=20]
Employment [name=诸葛, salary=2000, age=20]
Employment [name=刘备, salary=3000, age=20]

------------------------------------------------------------------------------------------------------------------------------苦难不是博得同情的资本,只有不断奋斗才能改变命运


Comparator和Comparable接口,布布扣,bubuko.com

Comparator和Comparable接口

标签:java   comparator compable   

原文地址:http://blog.csdn.net/u012427937/article/details/38367331

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!