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 ...
随机推荐
- AD10长方形通孔焊盘的画法
1.点击工具栏中[放置焊盘]按钮 2.按键盘Tab键弹出[焊盘]对话框 3.设置[空洞信息]相关尺寸(根据自己所需实际设置) 这里左边的单选按钮选择“槽”,通孔尺寸输入20mil,长度为80mil,旋 ...
- Socket模型详解(转)
两种I/O模式 一.选择模型 二.异步选择 三.事件选择 四.重叠I/O模型 五.完成端口模型 五种I/O模型的比较 两种I/O模式 1. 两种I/O模式 阻塞模式:执行I/O操作完成前会一直进行等待 ...
- Android对话框之dismiss和cancel和hide区别
在我们看来两者效果都是一样的,其实看下源码就知道cancel肯定会去调dismiss的,如果调用的cancel的话就可以监听DialogInterface.OnCancelListener. /** ...
- Android working with Volley
Volley是google官方推出的一个开源网络访问库,在项目中直接使用它,而不需要写一大堆的重复的代码; 项目主页: https://android.googlesource.com/platfor ...
- php socket
socket demo代码如下 : server: server.php <?php //phpinfo(); //确保在连接客户端时不会超时 set_time_limit(0); $ip = ...
- Java-基本的程序设计结构
Java-基本的程序设计结构 >注释 Java的注释分为三种情况 第一种://开头 第二种:"/*" 开头 "*/"结尾 上面两种情况跟C#.C++.Ob ...
- undefined function mysql_connect()解决方法
在配置apache+php+mysql后,打开一个php网页文件正常,但是php网页中连接数据库时,出现以下提示: Fatal error: Call to undefined function my ...
- jquery.uploadify 异常 “__flash__removeCallback”未定义
使用场景结合artdialog弹出框使用时发生“__flash__removeCallback”未定义,原因在于artdialog基于iframe加载的uloadify,在关闭artdialog的时候 ...
- android sqlite导入数据
@Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { // TODO Auto-generated meth ...
- grunt压缩多个js文件和css文件
压缩前的工程目录: 1.安装js,css需要的插件 使用npm安装:npm install grunt-contrib-uglify --save-dev -------->安装js压缩插件 ...