Comparable与Comparator区别联系

Comparable 和Comparator都是比较接口,实现本接口就可以进行排序。


1.Comparable 在java.lang包中,Comparator位于java.util包中。


2. 

implements Comparable 需要实现

public abstract int compareTo (T another)
Added in  API level 1

Compares this object to the specified object to determine their relative order.

Parameters
another the object to compare to this instance.
Returns
  • a negative integer if this instance is less than another; a positive integer if this instance is greater than another; 0 if this instance has the same order as another.
Throws
ClassCastException if another cannot be converted into something comparable to this instance.


实现Comparator需要实现

public abstract int compare (T lhs, T rhs)
Added in  API level 1

Compares the two specified objects to determine their relative ordering. The ordering implied by the return value of this method for all possible pairs of (lhs, rhs) should form an equivalence relation. This means that

  • compare(a,a) returns zero for all a
  • the sign of compare(a,b) must be the opposite of the sign of compare(b,a) for all pairs of (a,b)
  • From compare(a,b) > 0 and compare(b,c) > 0 it must follow compare(a,c) > 0 for all possible combinations of (a,b,c)

Parameters
lhs an Object.
rhs a second Object to compare with lhs.
Returns
  • an integer < 0 if lhs is less than rhs, 0 if they are equal, and > 0 if lhs is greater than rhs.
Throws
ClassCastException if objects are not of the correct type.


由以上方式可以看出,一般Comparable 在类构造的时候就实现本接口,表示本类型的对象是可以比较的。

如果没有实现的话,又需要排序的话,就需要Comparator来定义两个对象的比较方式。


使用代码说明下,代码摘自      http://blog.csdn.net/happy492/article/details/6067910

package com.j2se.demo;

import java.util.Arrays;
/**
 * java.lang.comparable 此接口强行对实现它的每个类的对象进行整体排序。
 * 这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法。
 * Arrays.sort(Object[])根据元素的自然顺序对指定对象数组按升序进行排序。
 * 数组中的所有元素都必须实现 Comparable 接口。
 * @author edwin
 *
 */
public class ComparableUser implements Comparable {
	private String id;
	private int age;
	
	public ComparableUser(String id, int age){
		this.id = id;
		this.age = age;
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public int compareTo(Object o) {
		return this.age - ((ComparableUser)o).getAge();
	}
	
	public static void main(String[] args){
		ComparableUser[] users = new ComparableUser[]{
				new ComparableUser("id_1",23),
				new ComparableUser("id_2",20),
				new ComparableUser("id_3",25),
				new ComparableUser("id_4",19)
		};
		Arrays.sort(users);
		for(ComparableUser user : users){
			System.out.println("id="+user.getId()+" age="+user.getAge());
		}
	}
}


Compartor使用:

package com.j2se.demo;

public class User {
	private String id;
	private int age;
	
	public User(String id, int age){
		this.id = id;
		this.age = age;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

package com.j2se.demo;

import java.util.Arrays;
import java.util.Comparator;
/**
 * java.util.Comparator强行对某个对象 collection 进行整体排序 的比较函数。
 * Arrays.sort(Object[]a,Comparator c)根据指定比较器产生的顺序
 * 对指定对象数组进行排序。 数组中的所有元素都必须是通过指定比较器可相互比较的
 * @author edwin
 *
 */
public class ComparatorUser implements Comparator {

	@Override
	public int compare(Object o1, Object o2) {
		return ((User) o1).getAge() - ((User) o2).getAge();
	}

	public static void main(String[] args){
		User[] users = new User[]{
				new User("id_1",38),
				new User("id_2",29),
				new User("id_3",39),
				new User("id_4",20)
		};
		Arrays.sort(users, new ComparatorUser());
		for(User user : users){
			System.out.println("id="+user.getId()+" age="+user.getAge());
		}
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值