39-java中Arrays.sort 和 collections.sort()总结
总结一下java 中的两种排序工具:
Arrays.sort() : 主要针对 数组类型排序,如果数组里面的元素是对象,要按对象属性排序的话,需要重写 Comparator() 函数,重写里面的 int compare()函数;
Collections.sort(): 主要是针对集合排序,如:list。 当然如果数组 array[i] 的每项是一个list 也可以直接用。与Arrays.sort()一样,重写 Comparator()函数。
注意:两种方式重写的函数方式是一样的,单词不要写错哟: Comparator,compare
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class Main1 {
public static void main(String[] args) {
man[] mans = new man[3];
mans[0] = new man(1);
mans[1] = new man(24);
mans[2] = new man(5); Comparator<man> cmp = new My_Comparator ();
//对象数组排序:
Arrays.sort(mans, cmp);
for(man i : mans) {
System.out.print(i.a + ", ");
}
System.out.println("---------"); //对象链表排序:
List<man> list = new ArrayList<>();
list.add(new man(3));
list.add(new man(2));
list.add(new man(1));
// Collections.sort(list, new Comparator<man>() {
// public int compare(man x, man y) {
// return y.a - x.a;
// }
// });
// 将上面的简化为下面的:
Collections.sort(list, cmp);
for(man i : list) {
System.out.print(i.a + "; ");
}
}
}
class My_Comparator implements Comparator<man>{
@Override
public int compare(man o1, man o2) {
return o2.a - o1.a;
}
}
class man{
public int a;
man(int x){
this.a = x;
}
}
39-java中Arrays.sort 和 collections.sort()总结的更多相关文章
- Arrays.sort 与 Collections.sort
代码如下: package com.wangzhu.arrays; import java.util.Arrays; import java.util.Collections; public clas ...
- 48- java Arrays.sort和collections.sort()再次总结
今天又碰到一个新BUG,记下来. 一直报空指针异常,我就很奇怪了,怎么就空指针了呢,我输出时,也能输出东西呀. 原来Arrays.sort() 和 Collections.sort() 都是对整个数组 ...
- Arrays.sort和Collections.sort实现原理解析
Arrays.sort和Collections.sort实现原理解析 1.使用 排序 2.原理 事实上Collections.sort方法底层就是调用的array.sort方法,而且不论是Collec ...
- sort()排序 collections.sort();
1.main方法: public class Test { public static void main(String[] args) { /** * * sort()方法详解 * 1.Collec ...
- Java中Arrays.sort()和Collections.sort()
1.简单示例 sort方法的使用非常的简单明了,下面的例子中,先定义一个比较Dog大小的Comparator,然后将其实例对象作为参数传给sort方法,通过此示例,你应该能够快速掌握Arrays.so ...
- 关于Java中Arrays.sort()方法TLE
最近一直在练用Java写题,今天无意发现一道很简单的二分题(链接),我一开始是直接开int[]数组调用Arrays.sort()去排序,没想到TLE了,原来是因为jdk中对于int[]的排序是使用快速 ...
- 5.4 集合的排序(Java学习笔记)(Collections.sort(),及Arrays.sort()底层分析)
1.Comparable接口 这个接口顾名思义就是用于排序的,如果要对某些对象进行排序,那么该对象所在的类必须实现 Comparabld接口.Comparable接口只有一个方法CompareTo() ...
- java中Arrays.sort()对二位数组进行排序
int [][]a = new int [5][2]; //定义一个二维数组,其中所包含的一维数组具有两个元素 对于一个已定义的二位数组a经行如下规则排序,首先按照每一个对应的一维数组第一个元素进行升 ...
- 集合(一)-Java中Arrays.sort()自定义数组的升序和降序排序
默认升序 package peng; import java.util.Arrays; public class Testexample { public static void main(Stri ...
随机推荐
- delphi 新版内存表 FDMemTable
c++builder XE 官方demo最全60多个 http://community.embarcadero.com/blogs?view=entry&id=8761 FireDAC.Com ...
- Linux命令:chmod
https://baijiahao.baidu.com/s?id=1616750933810368135&wfr=spider&for=pc
- [Linux]Linux下动态安装PHP扩展的一般方法(图)
---------------------------------------------------------------------------------------------------- ...
- javaweb 学习系列【转】
http://www.cnblogs.com/xdp-gacl/category/574705.html jsp指令 http://www.cnblogs.com/huiyuantang/p/5332 ...
- 远程批量查看windosws操作系统3389端口的开放情况
本文只提供思想.具体可以根椐情况拓展. 前提是需要配置远程主机的SNMP协议.主要是共同体哟. 脚本使用: 1.拷贝check_tcp到脚本执行的主机中或在此主机中安装nagios; 2.保持list ...
- 微服务之间的调用(Ribbon与Feign)
来源:https://blog.csdn.net/jrn1012/article/details/77837658 使用Eureka作为服务注册中心,在服务启动后,各个微服务会将自己注册到Eureka ...
- 本地Navicat连不上Linux虚拟机MySQL数据库问题
LinuxAndMySQL 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux登录到MySQL数据:mysql -uroot -p 输入密码 切换到mysql数据库 mysql> ...
- DM 9000网卡驱动移植
1. 由于内核已经带有DM9000 网卡的驱动,所以主要移植工作是在板文件中添加 platform_device 结构,并加入 ok6410_devices[] __initdata 数组. 代码如下 ...
- Binary Logging Formats
[Binary Logging Formats] The server uses several logging formats to record information in the binary ...
- Photoshop Keynote
[Photoshop Keynote] 1.Tab:隐藏.显示所有面板. 2.Sihft+Tab:隐藏.显示右侧面板. 3.F:全屏切换. 4.选择并遮住: 参考:http://www.51shipi ...