java中Collections.sort排序詳解
來源:程序員人生 發布時間:2015-04-03 08:47:56 閱讀次數:3959次
Comparator是個接口,可重寫compare()及equals()這兩個方法,用于比價功能;如果是null的話,就是使用元素的默許順序,如a,b,c,d,e,f,g,就是a,b,c,d,e,f,g這樣,固然數字也是這樣的。
compare(a,b)方法:根據第1個參數小于、等于或大于第2個參數分別返回負整數、零或正整數。
equals(obj)方法:僅當指定的對象也是1個 Comparator,并且強行實行與此 Comparator 相同的排序時才返回 true。
Collections.sort(list, new PriceComparator());的第2個參數返回1個int型的值,就相當于1個標志,告知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 + " " + name; // 定義顯示類信息的字符串
DecimalFormat formatPrice = new DecimalFormat("0.00");// 格式化價格到小數點后兩位
showStr += " " + formatPrice.format(price);// 格式化價格
showStr += " " + author;
SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");
showStr += " " + 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, "3國演義", 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, "天龍8部", 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() + " 庫存:" + 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, "3國演義", 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, "天龍8部", 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(" " + 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);
}
}
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈