Collections 对List排序
来源:互联网 发布:免费语音朗读软件 编辑:程序博客网 时间:2024/05/17 23:21
通过 Collections 中的 sort() 方法对 List 进行排序
-------------------------Collections 中的sort方法:
tags 1:
static <T extends Comparable<? super T>> void sort(List<T> list)根据元素的自然顺序 对指定列表按升序进行排序。
tags 2:
static <T> void sort(List<T> list, Comparator<? super T> c)根据指定比较器产生的顺序对指定列表进行排序。
-------------------------
提示:
List 特点
1) 有序(按照元素添加顺序进行排列)
2) 可重复
---------------------------------------------------------------------------------------
下面以 将 ArrayList中的元素进行排序为例
一般来说,类中需要实现comparable接口,同时重写compareTo方法,才能确定排序方式
但 String、Integer、Double 等类都已完成了上述功能,故可直接使用,默认从小到大,字典序排序
如果自定义排序方式,则要创建比较器(通过匿名对象类)实现 Comparator接口,重写compare方法
1: 集合内元素为 Integer、String、Character等等
<span style="font-size:18px;"> List<String> list=new ArrayList<String> ();</span>第一种: 按照自然顺序排序
Collections.sort(list);
第二种: 自定义排序方式
eg: 按照字典序的反序进行排序
1)匿名对象类实现 Comparator 接口
2)重写compare方法
<span style="font-size:18px;"> Collections.sort(new Comparator<String>(){ @Override public int compare(String s1,String s2){ int num=Math.min(s1.length(), s2.length()); int i=0; while(i<num){ int tmp=s2.charAt(i)-s1.charAt(i); if(tmp!=0) return tmp; i++; } if(s1.length()!=s2.length()) return 1; else return 0; } });</span>
2: 集合内元素为自定义类对象
eg:集合内元素为 Student类
<span style="font-size:18px;"> List<Student> list=new ArrayList<Student> ();</span>方法 和 TreeSet 的三种实现方式相同
1)自然排序(其实也是自定义)
i: 在 Student类上实现Comparable接口
<span style="font-size:18px;">class Student implements Comparable<Student>{}<span style="font-size:18px;">如果同时有自然排序和比较器排序,则为比较器排序中的方式</span></span>ii:重写 compareTo 方法
<span style="font-size:18px;"> Collections.sort(list);</span>2)创建比较器类
i : 创建比较器类 MyComparator 实现 Comparator 接口
<span style="font-size:18px;"> public class MyComparator implements Comparator<Student>{}</span>
ii: 重写 compare 方法
<span style="font-size:18px;"> Collections.sort(list,MyComparator);</span>
3)创建匿名对象类
<span style="font-size:18px;"> Collections.sort(new Comparator<Student>{ @Override public int compare(Student s1,Student s2){ ... } });</span>注意:
上述这样做的好处
通过List集合可以存储重复元素,通过Collections的sort方法进行排序,还能自定义排序方式
等同于 弥补了 TreeSet 集合元素不能重复的缺点,弥补了 List 集合不能排序的缺点
结合了两者的优点,使得排序更加自由,限制缩小
0 0
- Collections 对List排序
- Collections.sort() 对 List 排序
- Collections.sort() 对 List 排序
- Collections.sort() 对 List 排序
- Comparator,Collections对List排序!
- Collections.sort() 对 List 排序
- Collections.sort() 对 List 排序
- Collections.sort() 对 List 排序
- Collections.sort() 对 List 排序
- Collections.sort() 对 List 排序
- Collections 对List 进行排序
- list---java List 排序 Collections.sort() 对 List 排序
- 使用Collections对list进行排序
- java Collections.sort() 对 List 排序
- 用Collections.sort方法对list排序
- 利用Collections.sort()方法对list排序
- java的Collections对list排序
- 自定义规则 Collections.sort() 对 List 排序
- hdu1713相遇周期
- 用GitHub和Hexo搭建个人博客
- CCPC[长春] 6.Harmonic Value Description 规律+贪心
- linux入门练习题之权限
- implicit declaration of function-linux C文件编译warning解决方法
- Collections 对List排序
- [数据结构]c语言实现链栈的入栈,出栈,清空,销毁等操作
- iOS上的Popover
- Java Web测试题(二)
- 新建索引的一些原则
- Unity3D的四种坐标系详细介绍
- 罗永浩失败的原因
- Android开发艺术探索读书笔记 第一章 Activity的生命周期和启动模式
- 打造自己的MVC 4-1 4-2