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 ...
随机推荐
- jquery $.proxy使用
在某些情况下,我们调用Javascript函数时候,this指针并不一定是我们所期望的那个.例如: //正常的this使用 $('#myElement').click(function() { // ...
- 一个自动备份mysql数据库的bat文件内容
自动备份mysql数据库,并已当前的日期时间为目录 copy过去, xcopy将近15年没有用dos命令,还是这么亲切 另 本方法是备份数据库文件,不是dump导出,然后再计划任务中使用,我用的是wa ...
- 小兔伴伴家庭动物园AR智能早教产品上市
2016年6月,经过乐卓大家庭所有人的共同努力,公司旗下首款新品——小兔伴伴之<家庭动物园>3D智能学习卡正式面世. 每个孩子都应该在合适的时间去体验丰富的声音.色彩和动作,<家庭动 ...
- Unity3d导出Android的apk文件时相关问题的解决办法
今天上午着手将一个unity3d开发的小游戏build到android手机上运行,结果遇到了不少问题. 首先遇到的第一个问题是在build到一半的时候,弹出如下报错: Error building P ...
- smartjs - DataManager API
dataServices 数据服务的管理器:首先看下具体的代码 //数据服务 dataServices = st.factory({ name: "dataServices", p ...
- MongoDB 安装记录
之前使用一直没记录,防再次掉坑,记录下 echo 开始 D: cd D:\Program Files\MongoDB\Server\3.2\bin mongod --install --service ...
- ActionBarSherlock SlidingMenu整合,解决SlidingMenu example的getSupportActionBar()方法不能用问题
今天下载了SlidingMenu来研究,发现里面那个自带的example不能使用,总是提示BaseActivity 里面找不到getSupportActionBar()方法,到Github上面一查果然 ...
- Linux高级编程--06.进程概述
进程控制块 在Linux中,每个进程在内核中都有一个进程控制块(PCB)来维护进程相关的信息,它通常包含如下信息: 进程id.系统中每个进程有唯一的id,在C语言中用pid_t类型表示,其实就是一个非 ...
- java中产生对象的两种方式
/* * 普通new对象的过程! */ Person pp = new Person(); System.out.println(pp); /* * 利用代用参数的构造器产生对象实例! * 首先获得相 ...
- Java定位CPU使用高问题--转载
原文地址:http://www.cnblogs.com/guoyuqiangf8/p/3545687.html 1.TOP命令,查询消耗CPU高的进程号 PID,并记录下来,按下键盘"H&q ...