[转]java中Collections.sort排序详解
Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能;如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f,g这样,当然数字也是这样的。
compare(a,b)方法:根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
equals(obj)方法:仅当指定的对象也是一个 Comparator,并且强行实施与此 Comparator 相同的排序时才返回 true。
Collections.sort(list, new PriceComparator());的第二个参数返回一个int型的值,就相当于一个标志,告诉sort方法按什么顺序来对list进行排序。
具体实现代码方法如下:
Book实体类:
- package com.tjcyjd.comparator;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.GregorianCalendar;
- import java.util.Iterator;
- import java.util.TreeMap;
- /**
- * 书实体类
- *
- * @author yjd
- *
- */
- public class Book implements Comparable { // 定义名为Book的类,默认继承自Object类
- public int id;// 编号
- public String name;// 名称
- public double price; // 价格
- private String author;// 作者
- public GregorianCalendar calendar;// 出版日期
- public Book() {
- this(0, "X", 0.0, new GregorianCalendar(), "");
- }
- public Book(int id, String name, double price, GregorianCalendar calender,
- String author) {
- this.id = id;
- this.name = name;
- this.price = price;
- this.calendar = calender;
- this.author = author;
- }
- // 重写继承自父类Object的方法,满足Book类信息描述的要求
- public String toString() {
- String showStr = id + "\t" + name; // 定义显示类信息的字符串
- DecimalFormat formatPrice = new DecimalFormat("0.00");// 格式化价格到小数点后两位
- showStr += "\t" + formatPrice.format(price);// 格式化价格
- showStr += "\t" + author;
- SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");
- showStr += "\t" + formatDate.format(calendar.getTime()); // 格式化时间
- return showStr; // 返回类信息字符串
- }
- public int compareTo(Object obj) {// Comparable接口中的方法
- Book b = (Book) obj;
- return this.id - b.id; // 按书的id比较大小,用于默认排序
- }
- public static void main(String[] args) {
- Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,
- 01, 25), "曹雪芹、高鄂");
- Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,
- 8), "罗贯中 ");
- Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,
- 28), "施耐庵 ");
- Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,
- 8), "吴承恩");
- Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,
- 23), "搜狐");
- TreeMap tm = new TreeMap();
- tm.put(b1, new Integer(255));
- tm.put(b2, new Integer(122));
- tm.put(b3, new Integer(688));
- tm.put(b4, new Integer(453));
- tm.put(b5, new Integer(40));
- Iterator it = tm.keySet().iterator();
- Object key = null, value = null;
- Book bb = null;
- while (it.hasNext()) {
- key = it.next();
- bb = (Book) key;
- value = tm.get(key);
- System.out.println(bb.toString() + "\t库存:" + tm.get(key));
- }
- }
- }
自定义比较器和测试类:
- package com.tjcyjd.comparator;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.GregorianCalendar;
- import java.util.Iterator;
- import java.util.List;
- public class UseComparator {
- public static void main(String args[]) {
- List<Book> list = new ArrayList<Book>(); // 数组序列
- Book b1 = new Book(10000, "红楼梦", 150.86, new GregorianCalendar(2009,
- 01, 25), "曹雪芹、高鄂");
- Book b2 = new Book(10001, "三国演义", 99.68, new GregorianCalendar(2008, 7,
- 8), "罗贯中 ");
- Book b3 = new Book(10002, "水浒传", 100.8, new GregorianCalendar(2009, 6,
- 28), "施耐庵 ");
- Book b4 = new Book(10003, "西游记", 120.8, new GregorianCalendar(2011, 6,
- 8), "吴承恩");
- Book b5 = new Book(10004, "天龙八部", 10.4, new GregorianCalendar(2011, 9,
- 23), "搜狐");
- list.add(b1);
- list.add(b2);
- list.add(b3);
- list.add(b4);
- list.add(b5);
- // Collections.sort(list); //没有默认比较器,不能排序
- System.out.println("数组序列中的元素:");
- myprint(list);
- Collections.sort(list, new PriceComparator()); // 根据价格排序
- System.out.println("按书的价格排序:");
- myprint(list);
- Collections.sort(list, new CalendarComparator()); // 根据时间排序
- System.out.println("按书的出版时间排序:");
- myprint(list);
- }
- // 自定义方法:分行打印输出list中的元素
- public static void myprint(List<Book> list) {
- Iterator it = list.iterator(); // 得到迭代器,用于遍历list中的所有元素
- while (it.hasNext()) {// 如果迭代器中有元素,则返回true
- System.out.println("\t" + it.next());// 显示该元素
- }
- }
- // 自定义比较器:按书的价格排序
- static class PriceComparator implements Comparator {
- public int compare(Object object1, Object object2) {// 实现接口中的方法
- Book p1 = (Book) object1; // 强制转换
- Book p2 = (Book) object2;
- return new Double(p1.price).compareTo(new Double(p2.price));
- }
- }
- // 自定义比较器:按书出版时间来排序
- static class CalendarComparator implements Comparator {
- public int compare(Object object1, Object object2) {// 实现接口中的方法
- Book p1 = (Book) object1; // 强制转换
- Book p2 = (Book) object2;
- return p2.calendar.compareTo(p1.calendar);
- }
- }
- }
[转]java中Collections.sort排序详解的更多相关文章
- java中Collections.sort排序详解
Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f, ...
- (网页)java中Collections.sort排序详解(转)
转自CSDN: Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b, ...
- Java中Collections.sort()排序详解
public static void main(String[] args) { List<String> list = new ArrayList<String>(); ...
- java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET
java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了! 社区福利快来领取免费参加MDCC大会机会哦 Tag功能介绍—我们 ...
- Java中的main()方法详解
在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...
- Java I/O : Java中的进制详解
作者:李强强 上一篇,泥瓦匠基础地讲了下Java I/O : Bit Operation 位运算.这一讲,泥瓦匠带你走进Java中的进制详解. 一.引子 在Java世界里,99%的工作都是处理这高层. ...
- 关于Java中Collections.sort和Arrays.sort的稳定性问题
一 问题的提出 关于Java中Collections.sort和Arrays.sort的使用,需要注意的是,在本文中,比较的只有Collections.sort(List<T> ele ...
- java中Collections.sort()方法实现集合排序
1.Integer/String泛型的List进行排序 List <Integer> integerlist = new ArrayList<Integer>(); //定 ...
- java中list和map详解
一.概叙 List , Set, Map都是接口,前两个继承至Collection接口,Map为独立接口, List下有ArrayList,Vector,LinkedList Set下有HashSet ...
随机推荐
- Android SDK的安装与环境变量的配置
配置Andriod环境变量前提是要先安装好JAVA环境 1.下载Android SDK,点击安装,放在任意不含空格.特殊符号和中文的路径即可. 2.默认路径安装后,安装完成,开始配置环境变量. 3.打 ...
- Feather包实现数据框快速读写,你值得拥有
什么是Feather? Feature是一种文件格式,支持R语言和Python的交互式存储,速度更快.目前支持R语言的data.frame和Python pandas 的DataFrame. Feat ...
- weblogic 包里面有中文文件名 会报错
目前:没有解决,只要有中文启动就报错 http://bbs.csdn.net/topics/10055670 http://www.2cto.com/os/201406/311394.html
- Supply
Supplier创建一个Supply Supply有tap或emit方法. 可以这样理解: Supplier创建一个工厂 Supply 用tap创建流水线 emit向流水线上传送加工品进行加厂 my ...
- PHP对象5: define / const /static
define定义全局常量: define('PATH', '/data/home/www'); const也是定义常量, 一般用于类中, 饰成员属性,不可以修饰方法,如下: class Test{ c ...
- flask插件系列之flask_celery异步任务神器
现在继续学习在集成的框架中如何使用celery. 在Flask中使用celery 在Flask中集成celery需要做到两点: 创建celery的实例对象的名字必须是flask应用程序app的名字,否 ...
- Delphi 10 seattle 去掉自带的代码连接线
- 不老的神器:安全扫描器Nmap渗透使用指南【转】
介绍 nmap是用来探测计算机网络上的主机和服务的一种安全扫描器.为了绘制网络拓扑图Nmap的发送特制的数据包到目标主机然后对返回数据包进行分析.Nmap是一款枚举和测试网络的强大工具. 特点 主机探 ...
- URAL题解二
URAL题解二 URAL 1082 题目描述:输出程序的输入数据,使得程序输出"Beutiful Vasilisa" solution 一开始只看程序的核心部分,发现是求快排的比较 ...
- MySQL-事务特性
1. 事务概念引入: 现实生活中,我们往往经常会进行转账操作,转账操作可以分为两部分来完成,转入和转出.只有这两部分都完成了才可以认为是转账成功.在数据库中,这个过程是使用两条语句来完成的,如果其中任 ...