List排序Collections.sort 重写compare
static List<Integer> intList = Arrays.asList(2,5,7, 3, 1);
public static void main(String[] args) {
System.out.println("before sort:");
PrintUtil.showList(intList);
System.out.println("=========================");
Collections.sort(intList,new Comparator<Integer>() {
public int compare(Integer o1, Integer o2) {
// 返回值为int类型,大于0表示正序,小于0表示逆序
System.out.println("o2-o1:"+(o2-o1)+"========o2="+o2+"o1="+o1);
if(o2>o1){
return -1;
}else{
return 1;
}
}
});
System.out.println("after sort:");
PrintUtil.showList(intList);
}
根据需求排序,方法内比较两个对象的参数哪个优先,返回值为int类型,大于0表示正序,小于0表示逆序
List排序Collections.sort 重写compare的更多相关文章
- 对List数组进行排序 Collections.sort(param1,param2)
@SuppressWarnings("unchecked") List<PageData> group_items_list = (List<PageData&g ...
- ht-8 对arrayList中的自定义对象排序( Collections.sort(List<T> list, Comparator<? super T> c))
package com.iotek.set; import java.util.ArrayList; import java.util.Collections; import java.util.Co ...
- java List 排序 Collections.sort() 对 List 排序
class User { String name; String age; public User(String name,String age){ this.name=name; this.a ...
- java List 排序 Collections.sort()
用Collections.sort方法对list排序有两种方法 第一种是list中的对象实现Comparable接口,如下: /** * 根据order对User排序 */ public class ...
- sort()排序 collections.sort();
1.main方法: public class Test { public static void main(String[] args) { /** * * sort()方法详解 * 1.Collec ...
- JAVA对list集合进行排序Collections.sort()
对一个集合中的对象进行排序,根据对象的某个指标的大小进行升序或降序排序.代码如下: // 进行降序排列 Collections.sort(list, new Comparator<ResultT ...
- Java-小技巧-006 List 排序 Collections.sort()
一.数组操作类:Arrays 最早开始使用,查看方法二分查找:public static int binarySearch(数据类型[] a, 数据类型 key) 前提是有序的 equals 先排序 ...
- 关于Collections.sort()排序方法的源码探索
/**下面在自己代码中使用Collections.sort()方法去比较Student对象,通过在自己写的类里面通过匿名内部类实现Comparator接口,这个接口是让你自己实现比较器的规则*/ // ...
- 5-java 排序, sort, collections.sort()
https://blog.csdn.net/whp1473/article/details/79678974 import java.util.ArrayList; import java.util. ...
随机推荐
- 【慕课网实战】Spark Streaming实时流处理项目实战笔记十七之铭文升级版
铭文一级: 功能1:今天到现在为止 实战课程 的访问量 yyyyMMdd courseid 使用数据库来进行存储我们的统计结果 Spark Streaming把统计结果写入到数据库里面 可视化前端根据 ...
- API网关设计(一)之Token多平台身份认证方案(转载)
原文:https://segmentfault.com/a/1190000018535570?utm_source=tag-newest 概述 今天咱们面对移动互联网的发展,系统一般是多个客户端对应一 ...
- Buffer cache hit ratio性能计数器真的可以作为SQL Server 内存瓶颈的判断指标吗?
SQL Server中对于Buffer cache hit ratio的理解: Buffer cache hit ratio官方是这么解释的:“指示在缓冲区高速缓存中找到而不需要从磁盘中读取的页的百分 ...
- 【算法】实现字典API:有序数组和无序链表
参考资料 <算法(java)> — — Robert Sedgewick, Kevin Wayne <数据结构> ...
- Mac键盘按键符号
通过修饰键来查看 像command.option.control.shift这些按键在Mac下叫做修饰键,一般情况下他们大都用来辅助输入或者用作快捷键的修饰键. 打开“系统偏好设置”,点击“键盘→键盘 ...
- Java中的代理机制
Java的三种代理模式 代理模式是一种设计模式,提供了对目标对象额外的访问方式,即通过代理对象访问目标对象,这样可以在不修改原目标对象的前提下,提供额外的功能操作,扩展目标对象的功能. 简言之,代理模 ...
- MongoDB 字符串值长度条件查询
在实际项目中常常会有根据字段值长度大小进行限制查询,例如查询商品名称过长或过短的商品信息,具体的实现方式可能有多种,在此记录常见的两种实现 使用 $where 查询(性能稍逊一些) 1 2 3 4 5 ...
- Linux 环境下使用g++编译C++
单个源文件生成可执行程序 下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码: /* helloworld.cpp */ #include <iostream& ...
- python绘图工具matplotlib在linux下安装和使用
工具介绍:matplotlib 是Python编程语言的一个绘图库及其数值数学扩展 NumPy.它为利用通用的图形用户界面工具包, 如Tkinter, wxPython, Qt或GTK+向应用程序嵌入 ...
- eclipse 安装插件报错问题
报错信息 An error occurred while installing the items session context was:(profile=epp.package.jee, phas ...