comparator接口与compare方法的实现
刷leetcodecode时看到一道题需要利用自定义的比较器进行排序,最开始一头雾水,看了API终于懂了~
Arrays.sort(T[] a,Comparator<? super T> c)可以根据比较器的compare方法对数组进行排序,compare方法的不同实现对应着不同的排序准则;
可以看到API中关于compare方法的解释如下:
int compare(T o1,T o2)
In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.
The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)
The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.
Finally, the implementor must ensure that compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z.
It is generally the case, but not strictly required that (compare(x, y)==0) == (x.equals(y)). Generally speaking, any comparator that violates this condition should clearly indicate this fact. The recommended language is "Note: this comparator imposes orderings that are inconsistent with equals."
- 参数:
o1- the first object to be compared.o2- the second object to be compared.- 也就是说compare方法根据其返回值确定比较对象的大小,如果返回值为正,认为o1>o2;返回值为负,认为o1<o2;返回值为0,认为两者相等;
-
public class SortByComparator{
//升序排列
public static void sortAscend(Integer[] a){
Arrays.sort(a, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return a.compareTo(b);
}
});
}
//降序排列,如果b.compareTo(a)>0,则compare方法根据其返回值认为a>b,与自然比较结果相反
public static void sortDescend(Integer[] a){
Arrays.sort(a, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b.compareTo(a);
}
});
}
public static void main(String[] args) {
Integer[] a={1,2,3};
//升序排列
sortAscend(a);
for(int num:a)
System.out.println(num);
//降序排列
sortDescend(a);
for(int num:a)
System.out.println(num); }
}输出
1
2
3
3
2
1注:Arrays.sort(T[] a,Comparator<? super T> c)默认为升序排列
comparator接口与compare方法的实现的更多相关文章
- Comparable和Comparator接口是干什么的?列出它们的区别。
Comparable和Comparator接口是干什么的?列出它们的区别. Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回 ...
- Java—集合框架 Collections.sort()、Comparable接口和Comparator接口
Collentions工具类--java.util.Collections Collentions是Java集合框架中,用来操作集合对象的工具类,也是Java集合框架的成员,与List.Map和Set ...
- Java中Comparator接口和Comparable接口的使用
普通情况下在实现对对象元素的数组或集合进行排序的时候会用到Comparator和Comparable接口,通过在元素所在的类中实现这两个接口中的一个.然后对数组或集合调用Arrays.sort或者Co ...
- Comparable和Comparator接口是干什么的?列出它们的区别
Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回负数,0,正数来表明输入对象小于,等于,大于已经存在的对象. Java提供了 ...
- java实现Comparable接口和Comparator接口,并重写compareTo方法和compare方法
原文地址https://segmentfault.com/a/1190000005738975 实体类:java.lang.Comparable(接口) + comareTo(重写方法),业务排序类 ...
- 比较器:Compare接口与Comparator接口区别与理解
一.实现Compare接口与Comparator接口的类,都是为了对象实例数组排序的方便,因为可以直接调用 java.util.Arrays.sort(对象数组名称),可以自定义排序规则. 不同之处: ...
- Java TreeSet集合排序 && 定义一个类实现Comparator接口,覆盖compare方法 && 按照字符串长度排序
package TreeSetTest; import java.util.Iterator; import java.util.TreeSet; import javax.management.Ru ...
- java学习-Comparable<Integer>接口方法的实现
基本数据类型的包装类Integer, Float, Double,Long,Byte等都实现的Comparable接口,用于列表List或数组arrays的排序 Comparable<Integ ...
- 关于comparator接口和comparable接口以及它们各自的方法compare()和compareTo()
在今天做的LeetCode的题中有两道都出现了利用接口实现对象的排序.两题的相关链接: 1.利用comparable接口对对象排序 2.利用comparator接口实现排序 因为之前都没接触过这两个接 ...
随机推荐
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
- 使用自定义签名的https的ssl安全问题解决和metro的webservice调用
最近一直在忙新的项目,每天加班到8点多,都没来写博客了.新的项目遇到了很多问题,现在趁着突然停电来记录下调用https的问题吧. 我们服务主要是,我们调用数据源数据,并且再提供接口供外部数据调用. 我 ...
- php常用array函数
http://www.w3school.com.cn/php/php_ref_array.asp 1.array_change_key_case() 把数组中所有键更改为小写或大写2.array_ch ...
- memcache基础知识
memcached的内存存储机制 Memcached默认情况下采用了名为Slab Allocator的机制分配.管理内存.在该机制出现以前,内存的分配是通过对所有记录简单地进行malloc和free来 ...
- Java中货币格式化
private final static NumberFormat CURRENCY_FORMAT = NumberFormat.getCurrencyInstance(Locale.CHINA); ...
- UITableViewCell内放置复杂结构的subview,但subview中的Button不响应点击事件
原因可能是subview的frame没设置或者不对,这种情况下subview的内容依然正常显示,但是无法接收点击响应的消息. 具体来说就是缺少了下面这句代码: // subview - (id)ini ...
- hibernateTools插件安装
先去官方网站工具页面 http://hibernate.org/tools/ 点击Download 然后出现 你可以选择在线安装 在eclipse里面.help->install->add ...
- AngularJS中多个ng-app(手动加载模块)
1.当有多个ng-app时:(首先是要加载angularJS) <div ng-app=""> <p>姓名:<input type="tex ...
- C#datagridview 防止闪烁的方法
描述:拉动DataGridView滚动条时会出现闪烁,看起来像是在重画,感觉不是很好. 解决方式:采用双缓存机制,在方案中添加方法. 代码: public static class Extension ...
- [poj2182] Lost Cows (线段树)
线段树 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacula ...