(网页)java中Collections.sort排序详解(转)
转自CSDN:
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(, "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(, "红楼梦", 150.86, new GregorianCalendar(,
, ), "曹雪芹、高鄂");
Book b2 = new Book(, "三国演义", 99.68, new GregorianCalendar(, ,
), "罗贯中 ");
Book b3 = new Book(, "水浒传", 100.8, new GregorianCalendar(, ,
), "施耐庵 ");
Book b4 = new Book(, "西游记", 120.8, new GregorianCalendar(, ,
), "吴承恩");
Book b5 = new Book(, "天龙八部", 10.4, new GregorianCalendar(, ,
), "搜狐");
TreeMap tm = new TreeMap();
tm.put(b1, new Integer());
tm.put(b2, new Integer());
tm.put(b3, new Integer());
tm.put(b4, new Integer());
tm.put(b5, new Integer());
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(, "红楼梦", 150.86, new GregorianCalendar(,
, ), "曹雪芹、高鄂");
Book b2 = new Book(, "三国演义", 99.68, new GregorianCalendar(, ,
), "罗贯中 ");
Book b3 = new Book(, "水浒传", 100.8, new GregorianCalendar(, ,
), "施耐庵 ");
Book b4 = new Book(, "西游记", 120.8, new GregorianCalendar(, ,
), "吴承恩");
Book b5 = new Book(, "天龙八部", 10.4, new GregorianCalendar(, ,
), "搜狐");
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);
}
}
}
原文:http://blog.csdn.net/tjcyjd/article/details/6804690
(网页)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排序详解
Comparator是个接口,可重写compare()及equals()这两个方法,用于比价功能:如果是null的话,就是使用元素的默认顺序,如a,b,c,d,e,f,g,就是a,b,c,d,e, ...
- 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 ...
随机推荐
- 利用ngnix解决跨域问题
一,定义 跨域是指从一个域名的网页去请求另一个域名的资源,它是由浏览器的同源策略造成的,是浏览器对JavaScript施加的安全限制.跨域的严格一点的定义是:只要 协议,域名,端口有任何一个的不同,就 ...
- webpack通过postcss-loader添加浏览器前缀
在webpack中,我们可以很方便的使用autoprefixer来为css3属性添加不同的浏览器前缀. 首先,需要安装autoprefixer不用多说了,其次是安装postcss-loader(npm ...
- resin远程调试配置
1.进入resin的安装路径下的conf目录,下面有个resin.conf的文件,打开它,将下面这段配置添加进去,然后重启resin(大家应该知道如何重启吧): <jvm-arg>-Xde ...
- Jenkins系列之三——centos7.4+GitLab+Jenkins部署
GitLab介绍 GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 系统环境准备 建议:内存4G以上不然带不动 [root@hejianl ...
- Kafka实战-数据持久化
1.概述 经过前面Kafka实战系列的学习,我们通过学习<Kafka实战-入门>了解Kafka的应用场景和基本原理,<Kafka实战-Kafka Cluster>一文给大家分享 ...
- Solidity中uint转string
在<Solidity中uint转bytes>中,我们知道unit如何转换成bytes,其实把uint转换成string,就是在最后加上string(bytes变量)即可,如下所示: pra ...
- sass重构响应式unofficial‘s博客轻松适应移动端
前言: 刚刚玩博客园几天时间,发现挺不错的,对于我这个懒人又是一个爱折腾的人来说挺不错的,对于上班玩电脑,下班玩手机的用户来说,博客园中我的博客有一点给我的感觉不是很友好,电脑端看起来很美观的一个页面 ...
- Scala面向对象编程与类型系统
Scala支持面向对象编程, 其面向对象特性与Java有共同之处并添加了很多新的特性. 类定义 scala使用class关键字定义类: class MyComplex(real0:Double, im ...
- MySQL事务笔记
1.结束事务的方法用什么? 2.事务的最终形态是什么? commit 提交 rollback 回滚 3.事务的四大特征? ⑴ 原子性 一个事务是最小的工作单元,事务包含的所有操作要么全部成功,要么全部 ...
- Linux进程间通信(System V) --- 共享内存
共享内存 IPC 原理 共享内存进程间通信机制主要用于实现进程间大量的数据传输,下图所示为进程间使用共享内存实现大量数据传输的示意图: 共享内存是在内存中单独开辟的一段内存空间,这段内存空间有自己特有 ...