Comparable 和Comparator都是比较接口,实现本接口就可以进行排序。
1.Comparable 在java.lang包中,Comparator位于java.util包中。
2.
implements Comparable 需要实现
Compares this object to the specified object to determine their relative order.
another | the object to compare to this instance. |
---|
another
; a positive integer if this instance is greater than another
; 0 if this instance has the same order as another
.ClassCastException | if another cannot be converted into something comparable to this instance. |
---|
实现Comparator需要实现
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
compare(a,b)
must be the opposite of the sign of compare(b,a)
for all pairs of (a,b)compare(a,b) > 0
and compare(b,c) > 0
it must follow compare(a,c) > 0
for all possible combinations of (a,b,c)
lhs | an Object . |
---|---|
rhs | a second Object to compare with lhs . |
lhs
is less than rhs
, 0 if they are equal, and > 0 if lhs
is greater than rhs
.ClassCastException | if objects are not of the correct type. |
---|
如果没有实现的话,又需要排序的话,就需要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()); } } }