详解Java中Collections.sort排序
作者:浮云中的毛驴 时间:2023-08-20 00:51:48
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排序网站的支持!
来源:http://blog.csdn.net/tjcyjd/article/details/6804690
标签:java,collections.sort,排序
0
投稿
猜你喜欢
C#时间戳基本用法实例分析
2022-10-12 14:22:40
C#泛型详解及关键字作用
2023-04-07 20:23:12
详解Java设计模式之桥接模式
2022-08-05 23:27:08
Spring-boot的debug调试代码实例
2023-10-17 04:49:01
基于Java语言MD5加密Base64转换方法
2023-11-24 00:22:41
spring缓存cache的使用详解
2023-03-28 11:36:14
Android实现支付宝支付密码输入界面
2023-06-20 14:57:15
C#中可枚举类型详解
2023-03-24 20:39:15
Android RecyclerView使用ListAdapter高效刷新数据的操作方法
2023-06-24 22:22:09
C#书写规范
2023-07-09 09:15:57
android自定义View实现简单五子棋游戏
2022-09-16 14:52:30
C# using语法糖图文详解
2023-08-05 02:57:36
Android UI控件之Gallery实现拖动式图片浏览效果
2023-02-06 13:22:01
Java排序的那些事之sort方法的使用详解
2023-01-22 00:18:35
C# 内部类与Lambda表达式用法详解
2022-07-13 05:54:11
@Configuration与@Component作为配置类的区别详解
2023-03-09 19:50:15
c# 获得局域网主机列表实例
2022-07-12 21:46:58
java中压缩文件并下载的实例详解
2022-01-01 04:59:12
Java剑指offer之删除链表的节点
2023-05-19 15:30:27
Spring整合mybatis实现过程详解
2022-05-23 02:10:23