Comparable接口和Comparator接口

Comparable接口中只申明了一个方法

* @param   o the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
*
* @throws NullPointerException if the specified object is null
* @throws ClassCastException if the specified object's type prevents it
* from being compared to this object.

public int compareTo(Object o)

类实现了Comparable接口说明这个类的对象是能够进行比较的,也就可以排序。

“实现Comparable接口的类的对象的List列表(或数组)”,则该List列表(或数组)可以通过 Collections.sort(或 Arrays.sort)进行排序。

此外,“实现Comparable接口的类的对象”可以用作“有序映射(如TreeMap)”中的键或“有序集合(TreeSet)”中的元素,而不需要指定比较器。

如String类就实现了comarable接口

public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;

int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}

 Comparator是一个函数式接口(@FunctionalInterface).所谓的函数式接口,当然首先是一个接口,然后就是在这个接口里面只能有一个抽象方法。加不加@FunctionalInterface对于接口是不是函数式接口没有影响,该注解只是提醒编译器去检查该接口是否仅包含一个抽象方法。Comparator接口中定义了一个抽象方法int compare(T o1, T o2);

* @param o1 the first object to be compared.
* @param o2 the second object to be compared.
* @return a negative integer, zero, or a positive integer as the
* first argument is less than, equal to, or greater than the
* second.
* @throws NullPointerException if an argument is null and this
* comparator does not permit null arguments
* @throws ClassCastException if the arguments' types prevent them from
* being compared by this comparator.
int compare(T o1, T o2);
还有一个equals方法,这个方法不当作抽象方法(函数式接口里是可以包含Object里的public方法,因为任何一个函数式接口的实现,默认都继承了Object类,包含了来自java.lang.Object里对这些抽象方法的实现;)
* @param   obj   the reference object with which to compare.
* @return <code>true</code> only if the specified object is also
* a comparator and it imposes the same ordering as this
* comparator.
* @see Object#equals(Object)
* @see Object#hashCode()
*/
boolean equals(Object obj);
此外还有一系列的defalt方法,如
default Comparator<T> reversed() {
return Collections.reverseOrder(this);
}
任何一个类要实现Comparator接口都要实现compare方法,但可以不实现equals方法。因为每个类的子类都默认是Object类。

Comparable是排序接口;若一个类实现了Comparable接口,就意味着“该类支持排序”。
而Comparator是比较器;我们若需要控制某个类的次序,可以建立一个“该类的比较器”来进行排序。

我们不难发现:Comparable相当于“内部比较器”,而Comparator相当于“外部比较器”。

 参考:https://www.cnblogs.com/skywang12345/p/3324788.html

public class CompariableAndComparatorTest {

public static void main(String[] args)
{
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person("a",10));
list.add(new Person("b",15));
list.add(new Person("c",12));
list.add(new Person("d",9));

System.out.printf("Original sort, list:%s\n", list);
Collections.sort(list);
System.out.printf("Name sort, list:%s\n", list);
Collections.sort(list,new AgeAscComparator());
System.out.printf("Name sort, list:%s\n", list);

}

private static class Person implements Comparable<Person>{
String name;
int age;
public Person(String name,int age)
{
this.name = name;
this.age = age;
}

public int compareTo(Person p) {
//return name.compareTo(p.name);
return this.age - p.age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

public void setName(String name) {
this.name = name;
}

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

@Override
public String toString() {
return this.name +"--"+this.age;
}

boolean equals(Person person) {
if (this.age == person.age && this.name == person.name)
return true;
return false;
}
}

private static class AgeAscComparator implements Comparator<Person> {

public int compare(Person o1, Person o2) {
return o1.getAge()-o2.getAge();
}
}
}






 

转载于:https://www.cnblogs.com/moxia1234/p/11386578.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值