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 ...
随机推荐
- caffe上使用hdf5格式文件以及回归(regression)问题
最近用caffe做了一下regression问题,先用data layer中的data,float_data试了一下,data用来存放图片,float_data存放regression的values, ...
- 跟随标准与Webkit源码探究DOM -- 获取元素之getElementsByTagName
按照标签名获取元素 -- getElementsByTagName 标准 DOM 1在Element和Document两个interface中均有定义,原型NodeList getElementsBy ...
- 在自己的框架中引用 PHPExcel
如果直接在框架中的controller中直接引用 xxxx/PHPExcel.php,由于框架中有autoload 与PHPExcel的autoload冲突(加载目录原因), 那么在不想做太多修改的情 ...
- APP-BOM-20516 错误处理一例
昨天在处理一个工单异常时,需要将一个Released的工单改为Unreleased状态,程序报APP-BOM-20516错误,如下图.百度只搜到两条记录,均无用.Google能搜到的多一些,也无用.进 ...
- Java知多少(112)数据库之删除记录
删除数据表也有3种方案 一.使用Statement对象 删除数据表记录的SQL语句的语法是: delete from 表名 where 特定条件 例如 : delete from ksInfo whe ...
- 关于C# Math 处理奇进偶不进
话说,最近一次系统维护 用JS读取导入Excel中的实验数据,出现被自动四舍五入.后来到客户现场听客户反馈 Excel实验数据要求 奇进偶不进. 关于 奇进偶不进 产生的由来:从统计学的角度,“奇进 ...
- 用JavaScript修改浏览器tab标题
修改tab或者window的标题,是一项较老的实践.Gmail 用它来提示用户新的聊天消息,当有新的page通过AJAX加载的时候,本站同样用它更新tab title.这是怎样做到的呢?当时是通过设置 ...
- Node.js 入门手册:那些最流行的 Web 开发框架
这篇文章与大家分享最流行的 Node.js Web 开发框架.Node 是一个服务器端 JavaScript 解释器,它将改变服务器应该如何工作的概念.它的目标是帮助程序员构建高度可伸缩的应用程序,编 ...
- jquery判断radioButton是否被选中
so easy HTML: <input type='radio' style='width:20px' id='other' name='projectType' value='其他' /&g ...
- mysqlbinlog -v --base64-output 与不加的区别
加-v与加-vv的区别: 加--base64-output=DECODE-ROWS与不加的区别: