ArrayList等常见集合的排序问题
对于ArrayList等常用的集合具体业务类,基本上都实现了Comparable接口,即可以用来比较装载的对象实体。
主要用Collections.sort方法对集合类中的对象进行排序
Collections.sort的两种重载方法
1.Collections.sort(list, comparator)方法,通过comparator规则,实现对list的特定排序。
2.Collections.sort(list),list中的对象自身实现comparator接口
Java集合框架:
代码示例(演示Collections.sort(list, comparator)方法):
注意:本代码均已在`jdk1.6`版本下通过测试
model,Student类
public class Student { private int id; private String name; private int age; /** * @Title: Student * @Description: TODO * @param: * @throws */ public Student(int id, String name, int age) { // TODO Auto-generated constructor stub this.id = id; this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } @Override public String toString() { return String.format("Student [age=%s, name=%s, id=%s]", age, name, id); } }
测试类
public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Student> arrayList = new ArrayList<Student>(); Student s1 = new Student(1, "jack", 20); Student s2 = new Student(2, "jack", 20); Student s3 = new Student(3, "lily", 29); Student s4 = new Student(4, "tom", 30); Student s5 = new Student(5, "rose", 31); Student s6 = new Student(6, "crane", 20); Student s7 = new Student(7, "jack", 25); Student s8 = new Student(8, "rose", 27); Student s9 = new Student(9, "lucy", 18); arrayList.add(s1); arrayList.add(s2); arrayList.add(s3); arrayList.add(s4); arrayList.add(s5); arrayList.add(s6); arrayList.add(s7); arrayList.add(s8); arrayList.add(s9); Comparator<Student> studentComparator = new Comparator<Student>() { /** * * @Title: compare * @Description: 先比较age,再比较name,最后比较id * @param: @param o1 * @param: @param o2 * @param: @return * @return: int * @throws */ @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub if (o1.getAge() != o2.getAge()) { return o1.getAge() - o2.getAge(); } else if (!o1.getName().equals(o2.getName())) { return o1.getName().compareTo(o2.getName()); } else if (o1.getId() != o2.getId()) { return o1.getId() - o2.getId(); } return 0; } }; Collections.sort(arrayList, studentComparator); for (Student student : arrayList) { System.out.println(student.toString()); } }
测试结果
Student [age=18, name=lucy, id=9] Student [age=20, name=crane, id=6] Student [age=20, name=jack, id=1] Student [age=20, name=jack, id=2] Student [age=25, name=jack, id=7] Student [age=27, name=rose, id=8] Student [age=29, name=lily, id=3] Student [age=30, name=tom, id=4] Student [age=31, name=rose, id=5]
ArrayList等常见集合的排序问题的更多相关文章
- c# 集合ArrayList;特殊集合Stack、Queue
一) ArrayList 1.foreach遍历数组中各个元素,执行内部语句 2. 3. 4. myarry.Clear();//将集合清空 bool b = myarry.Contains(3 ...
- JavaScript常见集合操作
JavaScript常见集合操作 集合的遍历 FOR循环(效率最高) 优点:JavaScript最普遍的for循环,执行效率最高 缺点:无法遍历对象 for(let i=0;i<array.le ...
- HashMap,Hashset,ArrayList以及LinkedList集合的区别,以及各自的用法
基础内容 容器就是一种装其他各种对象的器皿.java.util包 容器:Set, List, Map ,数组.只有这四种容器. Collection(集合) 一个一个往里装,Map 一对一对往里装. ...
- C#的常见集合接口提供的功能
C#的常见集合接口提供的功能 这里的功能都是泛型版本的常见功能,列出来,也许后面用得上吧,没有放非泛型版本,因为觉得用得不多,也就没有整理 IEnumerable<T> ICollecti ...
- ArrayList/List 泛型集合
List泛型集合 集合是OOP中的一个重要概念,C#中对集合的全面支持更是该语言的精华之一. 为什么要用泛型集合? 在C# 2.0之前,主要可以通过两种方式实现集合: a.使用ArrayList 直接 ...
- List、Set、Map常见集合遍历总结
Java中的集合有三大类,List.Set.Map,都处于java.util包中,List.Set和Map都是接口,不能被实例化,它们的各自的实现类可以被实例化.List的实现类主要有ArrayLis ...
- 2 Java中常见集合
1)说说常见的集合有哪些吧? 答:集合有两个基本接口:Collection 和 Map. Collection 接口的子接口有:List 接口.Set 接口和 Queue 接口: List 接口的实现 ...
- ArrayList,LinkedList,Vector集合的认识
最近在温习Java集合部分,花了三天时间读完了ArrayList与LinkedList以及Vector部分的源码.之前都是停留在简单使用ArrayList的API,读完源码看完不少文章后总算是对原理方 ...
- ArrayList , Vector 数组集合
ArrayList 的一些认识: 非线程安全的动态数组(Array升级版),支持动态扩容 实现 List 接口.底层使用数组保存所有元素,其操作基本上是对数组的操作,允许null值 实现了 Randm ...
随机推荐
- 初探设计:Java接口和抽象类何时用?怎么用?
今天犯了个错: “接口变动,伤筋动骨,除非你确定只有你一个人在用”.哪怕只是throw了一个新的Exception.哈哈,这是我犯的错误. 一.接口和抽象类 类,即一个对象. 先抽象类,就是抽象出类的 ...
- 锁屏上显示Activity
在Android中,有些比较强的提醒,需要用户紧急处理的内容.需要唤醒屏幕,甚至在锁定屏幕的情况下,也要显示出来.例如,来电界面和闹钟提醒界面.这是怎样实现的呢? 其实,实现起来非常简单.只要给Act ...
- Node快速安装
1.安装nvm nvm是一个快速安装和切换nodejs版本的管理器 直接从 github clone nvm 到本地, 这里假设大家都使用 ~/git 目录存放 git 项目: $ cd ~/git ...
- 【cs229-Lecture18】线性二次型调节控制
本节内容: 控制MDP的算法: 状态行动奖励: 非线性动力学系统: 模型: LQR:线性二次型调节控制:(Riccati方程)
- 暴力清除Android中的短信
有些短信程序有bug,当短信(特别是彩信)没有接收完整,或者是一些异常情况下,你会收到一条短信但是看不到或者看不了. 此时郁闷的事情就来了,系统会提醒你还有1条未读短信,但是你满世界都找不到这条短信. ...
- 论文第4章:iOS绘图平台的实现
面向移动设备的矢量绘图平台设计与实现 Design and Implementation of Mobile Device-oriented Vector Drawing Platform 引用本论文 ...
- 详解SPI中的极性CPOL和相位CPHA
SPI由于接口相对简单(只需要4根线),用途算是比较广泛,主要应用在 EEPROM,FLASH,实时时钟,AD转换器,还有数字信号处理器和数字信号解码器之间.即一个SPI的Master通过SPI与一个 ...
- ruby -- 进阶学习(九)定制错误跳转404和500
在开发阶段,如果发生错误时,都会出现错误提示页面,比如:RecordNotFound之类的,虽然这些错误方便开发进行debug,但是等产品上线时,如果还是出现这些页面,对于用户来说是很不友好的. 所以 ...
- Android 布局之LinearLayout
Android 布局之LinearLayout 1 LinearLayout简介 LinearLayout是线程布局.它包括2个方向(android:orientation):“水平”(horizon ...
- Python文件操作详解
Python内置了一个open()方法,用于对本地文件进行读写操作.这个功能简单.实用,属于必须掌握的基础知识. 使用open方法操作文件可以分三步走,一是打开文件,二是操作文件,三是关闭文件.下面分 ...