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 ...
随机推荐
- 浅析 mondrian 模式文件 Schema
1.前言 前面几篇文章一经介绍过saiku.模式文件和MDX的关系.通俗点说模式文件(Schema)就是一个xml,里面定义了一个虚拟立方体,共MDX查询语言使用. 2.模式文件 Schema 最顶层 ...
- WindowsCE project missing - 转
00x0 前言 之前在Windows 7系统中开发的WindowsCE项目,最近换成Windows 10系统,需要将项目进行修改,打开项目后提示如下错误: 无法读取项目文件“App.csproj”.. ...
- 跟随标准与Webkit源码探究DOM -- 获取元素之querySelector,querySelectorAll
使用CSS选择器获取元素 -- querySelector,querySelectorAll(HTML5) 标准 W3C Selector API Level 1为Document,DocumentF ...
- [转]SharePoint开发中可能用到的各种Context(上下文)
SharePoint是一个B/S结构的产品,所以在开发过程中会使用到各种各样的上下文(Context)信息,借此机会来总结一下.特别是Javascript的Ctx非常实用,这里记录一下! 一.Http ...
- 如何在IIS7或IIS7.5中导入导出站点及应用程序池.
为实现负载平衡,我们可能会使用多个WEB服务器,也就会需要给多个IIS配置同样的站点和应用程序池.那么我们需要一个一个的重新建吗?当然不用,我们只需要一些简单的命令就可以在IIS7(Windows S ...
- 二十三、【开源】EFW框架Web前端开发之常用组件(FusionCharts图表、ReportAll报表等)
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.2:http://pan.baidu.com/s/1hcnuA EFW框架实例源代码下载:http://pan ...
- 获取ACCESS数据库中自增变量的值
/// <summary> /// 获取自增变量值 /// </summary> /// <returns>自增变量值</returns> public ...
- java面试欠缺知识点总结
针对最近面试被问到的问题,总结自己欠缺的知识点,并要在接下来的1年内加强这些知识: Java方面:反射.线程concurrent包: Spring方面:Ioc和Aop.事务: 持久化框架:设计并实现分 ...
- 关于鼠标事件的screenY,pageY,clientY,layerY,offsetY属性 (详细图解)
screenY 鼠标相对于显示器屏幕左上角的偏移 pageY 鼠标相对于页面左上角的偏移 (其值不会受滚动条的影响) IE9之下并不支持这个属性 但是可以写点代码计算出来. jQuery中的实现: / ...
- 在Kibana上格式化字段,更好的在dashboard上展示
一.为什么要格式化? 接着之前的文章-利用 ELK系统分析Nginx日志并对数据进行可视化展示.下面是http访问的日志,里面有一个字段,bytes 传输的字节,如下图: 绿色框框内选中的就是本次请求 ...