Comparable Comparator equals
2015-02-03 19:00
113 查看
为了让类的对象间可以排序,通常有两种方法——Comparable<T> 与 Comparator<T>。
本对象与参数进行对比。返回为负表示小于,零表示相等,正数表示大于。
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.
返回值约定同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.
重写此方法达到想要的目的。
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)重写此方法达到想要的目的。
相关文章推荐
- TreeSet(有序集合)对Comparable元素的排序(或使用Comparator)与元素equals方法的关系
- java-comparable和comparator的区别
- 比较器Comparable与Comparator
- java中Comparable和Comparator
- 关于 Comparable 和 Comparator
- java中Comparable接口和Comparator接口的使用
- Comparable和Comparator
- java.util.Collections.sort(List list)类的应用(与Comparable,Comparator比较)
- IComparable IComparator用法
- 【慕课笔记】第六章 JAVA中的集合框架(下) 第7节 Comparable&Comparator简介
- Comparator和Comparable
- Comparable与Comparator的区别
- Java学习笔记--Comparable & Comparator
- Java中 Comparator接口 与Comparable 的区别
- comparator接口与Comparable接口的区别
- Strategy设计模式之Comparable&Comparator接口
- 【Java语言学习】之Comparator 和 Comparable 对比
- Comparable 和 Comparator的区别
- 接口 Comparable与Comparator的区别 (Java)
- Java 中Collection和Collections 、Comparable 和Comparator的区别