请注明出处:http://blog.csdn.net/qq_23179075/article/details/78753136

Java中Collections.sort()的使用!

在日常开发中,很多时候都需要对一些数据进行排序的操作。然而那些数据一般都是放在一个集合中如:MapSetList 等集合中。他们都提共了一个排序方法 sort(),要对数据排序直接使用这个方法就行,但是要保证集合中的对象是 可比较的

怎么让一个对象是 可比较的,那就需要该对象实现 Comparable<T> 接口啦。然后重写里面的
compareTo()方法。我们可以看到Java中很多类都是实现类这个接口的 如:IntegerLong 等等。。。

假设我们有一个学生类,默认需要按学生的年龄字段 age 进行排序 代码如下:

public class Student implements Comparable<Student> {private int id;private int age;private String name;public Student(int id, int age, String name) {this.id = id;this.age = age;this.name = name;}@Overridepublic int compareTo(Student o) {//降序//return o.age - this.age;//升序return this.age - o.age;        }@Overridepublic String toString() {return "Student{" +"id=" + id +", age=" + age +", name='" + name + '\'' +'}';}
}

这里说一下重写的 public int compareTo(Student o){} 这个方法,它返回三种 int 类型的值: 负整数正整数

返回值 含义
负整数 当前对象的值 < 比较对象的值 , 位置排在前
当前对象的值 = 比较对象的值 , 位置不变
正整数 当前对象的值 > 比较对象的值 , 位置排在后

测试代码:

public static void main(String args[]){List<Student> list = new ArrayList<>();list.add(new Student(1,25,"关羽"));list.add(new Student(2,21,"张飞"));list.add(new Student(3,18,"刘备"));list.add(new Student(4,32,"袁绍"));list.add(new Student(5,36,"赵云"));list.add(new Student(6,16,"曹操"));System.out.println("排序前:");for (Student student : list) {System.out.println(student.toString());}//使用默认排序Collections.sort(list);System.out.println("默认排序后:");for (Student student : list) {System.out.println(student.toString());}
}

输出日志:

排序前:
Student{id=1, age=25, name='关羽'}
Student{id=2, age=21, name='张飞'}
Student{id=3, age=18, name='刘备'}
Student{id=4, age=32, name='袁绍'}
Student{id=5, age=36, name='赵云'}
Student{id=6, age=16, name='曹操'}
默认排序后:
Student{id=6, age=16, name='曹操'}
Student{id=3, age=18, name='刘备'}
Student{id=2, age=21, name='张飞'}
Student{id=1, age=25, name='关羽'}
Student{id=4, age=32, name='袁绍'}
Student{id=5, age=36, name='赵云'}

比较器的使用

这个时候需求又来了,默认是用 age 排序,但是有的时候需要用 id 来排序怎么办? 这个时候比较器 :Comparator 就排上用场了。

Comparator 的使用有两种方式:

  • Collections.sort(list,Comparator<T>);
  • list.sort(Comparator<T>);

其实主要是看 Comparator 接口的实现,重写里面的 compare 方法。代码如下:

//自定义排序1
Collections.sort(list, new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return o1.getId() - o2.getId();}
});

compare(Student o1, Student o2) 方法的返回值跟 Comparable<> 接口中的 compareTo(Student o) 方法 返回值意思相同。另一种写法如下:

//自定义排序2
list.sort(new Comparator<Student>() {@Overridepublic int compare(Student o1, Student o2) {return o1.getId() - o2.getId();}
});

输出日志:

排序前:
Student{id=1, age=25, name='关羽'}
Student{id=2, age=21, name='张飞'}
Student{id=3, age=18, name='刘备'}
Student{id=4, age=32, name='袁绍'}
Student{id=5, age=36, name='赵云'}
Student{id=6, age=16, name='曹操'}
自定义排序后:
Student{id=1, age=25, name='关羽'}
Student{id=2, age=21, name='张飞'}
Student{id=3, age=18, name='刘备'}
Student{id=4, age=32, name='袁绍'}
Student{id=5, age=36, name='赵云'}
Student{id=6, age=16, name='曹操'}

Java中Collections.sort()的使用!相关推荐

  1. java中Collections.sort() 排序函数的用法

    java中Collections.sort() 排序函数的用法: 用Collections.sort方法对list排序有两种方法 第一种是list中的对象实现Comparable接口,如下: /** ...

  2. java中Collections.sort排序详解

    Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f, ...

  3. Java中Collections.sort()排序详解

    https://www.cnblogs.com/learnapi/p/9003112.html

  4. java中Arrays.sort()实现原理

    2019独角兽企业重金招聘Python工程师标准>>> 先在网上找到一些说法: java中Arrays.sort使用了两种排序方法,快速排序和优化的合并排序. 快速排序主要是对哪些基 ...

  5. java arrays.sort() c_正面刚算法-Java中Arrays.sort()(一)

    最近一直在看关于排序相关的算法,从O(n²)的冒泡.插入.选择到O(nlog(n))的归并.快排.再到桶排序.计数排序.基数排序.各个算法都有自己的优点和缺点,那么jdk中关于这种底层的算法是怎么实现 ...

  6. java中Collections常用方法总结(包括sort,copy,reverse等)

    1.sort(Collection)方法的使用(含义:对集合进行排序). 例:对已知集合c进行排序public class Practice {public static void main(Stri ...

  7. Java中Collections类概述和使用

    目录 Collections类概述 用Collections类的sort()方法对ArrayList集合元素排序 模拟斗地主过程中的洗牌,发牌和看牌 模拟斗地主升级版 1. Collections类概 ...

  8. Java 中使用sort排序

    刷题过程中常常遇到排序问题,Java中自带的sort方法可以非常方便的帮助我们进行排序. 常见的排序问题有两种情形: 1.对一个数组进行排序. 2.对自定义类型的类进行排序. 一,对数组进行排序: 通 ...

  9. Java中使用sort()进行升序降序排序

    这篇文章主要介绍了详解Java sort()数组排序(升序和降序),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 我们在学习 J ...

最新文章

  1. python正则表达式_Python正则表达式简记和re库!
  2. 公元2019年,你对AI的信任有几分?
  3. Linux下cp命令 -f无效的解决方法
  4. python where 多条件
  5. python处理excel表格实例-通过实例学习Python Excel操作
  6. java五子棋以当前空位为中心 取9个点_java 五子棋有点问题,哪位帮忙破一下、、...
  7. springsecurity sessionregistry session共享_不用 Spring Security 可否?试试这个小而美的安全框架...
  8. HP-UX平台安装Oracle11gR2数据库
  9. python2.7_1.13_编写一个SNTP客户端
  10. junit与testng 分别和mockito 结合使用例子
  11. 十万个为什么儿童版_把中国科技传播至阿语地区,少儿社《十万个为什么》阿拉伯文版亮相童书展...
  12. EditPlus自定义模板
  13. 博士毕业论文悲情致谢引女友回应:学术是一场超越金钱的修行
  14. MySQL常见面试题与答案
  15. ubuntu jdk tomcat mysql_Ubuntu下安装JDK+TOMCAT+MYSQL
  16. 机器学习之监督学习(四)——贝叶斯分类器
  17. EDA程序设计--出租车计费器
  18. 汇编DOS与Windows Masm编译运行代码步骤详解
  19. 《增长黑客》- 读书笔记(四)增长黑客循环
  20. 龙芯中科官方宣布,龙芯中科企业办公信息化平台全面完成国产化替代

热门文章

  1. 关闭计算机选项中没有休眠,win7系统电脑没有休眠功能的解决方法
  2. 流量暴增基建告急 网络重构需二次投资
  3. Redis高阶使用之Redisson分布式锁源码解析
  4. 干货向 - B 站 Up 主如何通过 Effie 快速地创作出一篇高质量视频文案?
  5. 为什么人们厌恶SharePoint
  6. Python中引号的用法
  7. 构建ceph deb 安装包
  8. 测试应届生是去自研小公司好还是外包公司好?
  9. ChatGPT“克星”:用AI识别AI生成的文本,英语论文阅读笔记都能测出
  10. 史上最详细Python爬取电影教程,还不会那也是没谁了