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 ... 
随机推荐
- tolua++实现分析
			项目正在使用cocos2dx的lua绑定,绑定的方式是tolua++.对大规模使用lua代码信心不是很足,花了一些时间阅读tolua++的代码,希望对绑定实现的了解,有助于项目对lua代码的把控.从阅 ... 
- debian+apache+acme_tiny+lets-encrypt配置笔记
			需要预先将需要申请ssl的域名指向到服务器,此方法完全通过api实现,好处是绿色无污染,不需要注册账号,不会泄露私人信息环境为 debian7+apache apt-get install apach ... 
- 关于app.config不能即时保存读取的解决方案
			public void saveValue(string Name, string Value) { ConfigurationManager.AppSettings.Set(Name, Value) ... 
- 全国DNS服务器IP地址【电信、网通、铁通】
			免费DNS地址: 114DNS:114.114.114.114(推荐国内使用) Google DNS:8.8.8.8(国外) ************************************* ... 
- LED子系统剖析
			写之前,先看一张图: 上次说了LED驱动程序,Linux自身也携带了LED驱动,且是脱离平台的,即LED子系统.操作起来十分简单.但是它的实质却不是那么容易,研究了一个晚上,终于明白了其中一个文件的功 ... 
- HANA Studio打开系统显示Secure storage is locked
			之前一直用着好好的,今天打开HANA Studio突然发现一个系统都不在了: 提示:‘Secure storage is locked’ 我点旁边的Unlocked没有任何反应,右键也没有功能了.如下 ... 
- 【问题与思考】1+"1"=?
			概述 在数学中1+1=2,在程序中1+1=2,而1+"1"=? 围绕着1+"1"的问题,我们来思考下这个问题. 目录: 一.在.Net代码中 二.在JavaSc ... 
- Uvaoj 11248 Frequency Hopping(Dinic求最小割)
			题意:1到n节点(节点之间有一定的容量),需要流过C的流量,问是否可以?如果可以输出possible, 否则如果可以扩大任意一条边的容量 可以达到目的,那么输出possible option:接着输出 ... 
- mockito
			import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWit ... 
- [IR] Index Construction
			Three steps to construct Inverted Index as following: 最难的step中: Token sequence. Sort by term. Dictio ... 
