您的位置:首页 > 其它

Comparable Comparator equals

2015-02-03 19:00 113 查看
为了让类的对象间可以排序,通常有两种方法——Comparable<T> 与 Comparator<T>。

Comparable泛型接口

int java.lang.Comparable.compareTo(T o)

本对象与参数进行对比。返回为负表示小于,零表示相等,正数表示大于。

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Comparator泛型接口

int StudentComparator.compare(Student o1, Student o2)

返回值约定同Comparable泛型接口。
当一个类没有实现Comparable接口而又希望对它排序时,可以用Comparator。

<Object> Comparator<Object> java.util.Collections.reverseOrder()

返回一个comparator,它的规则是自然顺序的倒序。自然顺序与类继承的Comparable接口实现有关。通常用于List的从大到小排列。

Returns a comparator that imposes the reverse of the natural ordering on a collection of objects that implement the Comparable interface.

equals重写

boolean Student.equals(Object o)
重写此方法达到想要的目的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: