java中的list 中sort排序結合compareTo方法的詳細介紹
1 /*測試類*/ 2 package test; 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.GregorianCalendar; 7 import java.util.Iterator; 8 import java.util.List; 9 10 public class UseComparator { 11 public static void main(String args[]) { 12 List<Book> list = new ArrayList<Book>(); // 數組序列 13 Book b1 = new Book(10000, "紅樓夢", 150.86, new GregorianCalendar(2009, 14 01, 25), "曹雪芹、高鄂"); 15 Book b2 = new Book(10001, "三國演義", 99.68, new GregorianCalendar(2008, 7, 16 8), "羅貫中 "); 17 Book b3 = new Book(10002, "水滸傳", 100.8, new GregorianCalendar(2009, 6, 18 28), "施耐庵 "); 19 Book b4 = new Book(10003, "西游記", 120.8, new GregorianCalendar(2011, 6, 20 8), "吳承恩"); 21 Book b5 = new Book(10004, "天龍八部", 10.4, new GregorianCalendar(2011, 9, 22 23), "搜狐"); 23 list.add(b1); 24 list.add(b2); 25 list.add(b3); 26 list.add(b4); 27 list.add(b5); 28 // Collections.sort(list); //沒有默認比較器,不能排序 29 System.out.println("數組序列中的元素:"); 30 myprint(list); 31 Collections.sort(list, new PriceComparator()); // 根據價格排序 32 System.out.println("按書的價格排序:"); 33 myprint(list); 34 Collections.sort(list, new CalendarComparator()); // 根據時間排序 35 System.out.println("按書的出版時間排序:"); 36 myprint(list); 37 } 38 39 // 自定義方法:分行打印輸出list中的元素 40 public static void myprint(List<Book> list) { 41 Iterator<Book> it = list.iterator(); // 得到迭代器,用於遍歷list中的所有元素 42 while (it.hasNext()) {// 如果迭代器中有元素,則返回true 43 System.out.println("\t" + it.next());// 顯示該元素 44 } 45 } 46 47 // 自定義比較器:按書的價格排序 48 static class PriceComparator implements Comparator<Object> { 49 public int compare(Object object1, Object object2) {// 實現接口中的方法 50 Book p1 = (Book) object1; // 強制轉換 51 Book p2 = (Book) object2; 52 return new Double(p1.price).compareTo(new Double(p2.price)); 53 } 54 } 55 56 // 自定義比較器:按書出版時間來排序 57 static class CalendarComparator implements Comparator<Object> { 58 public int compare(Object object1, Object object2) {// 實現接口中的方法 59 Book p1 = (Book) object1; // 強制轉換 60 Book p2 = (Book) object2; 61 return p2.calendar.compareTo(p1.calendar); 62 } 63 } 64 }
結果如下:
數組序列中的元素:
10000 紅樓夢 150.86 曹雪芹、高鄂 2009年02月25日
10001 三國演義 99.68 羅貫中 2008年08月08日
10002 水滸傳 100.80 施耐庵 2009年07月28日
10003 西游記 120.80 吳承恩 2011年07月08日
10004 天龍八部 10.40 搜狐 2011年10月23日
按書的價格排序:
10004 天龍八部 10.40 搜狐 2011年10月23日
10001 三國演義 99.68 羅貫中 2008年08月08日
10002 水滸傳 100.80 施耐庵 2009年07月28日
10003 西游記 120.80 吳承恩 2011年07月08日
10000 紅樓夢 150.86 曹雪芹、高鄂 2009年02月25日
按書的出版時間排序:
10004 天龍八部 10.40 搜狐 2011年10月23日
10003 西游記 120.80 吳承恩 2011年07月08日
10002 水滸傳 100.80 施耐庵 2009年07月28日
10000 紅樓夢 150.86 曹雪芹、高鄂 2009年02月25日
10001 三國演義 99.68 羅貫中 2008年08月08日
1 package test; 2 import java.text.DecimalFormat; 3 import java.text.SimpleDateFormat; 4 import java.util.GregorianCalendar; 5 import java.util.Iterator; 6 import java.util.TreeMap; 7 8 /** 9 * 書實體類 10 * 11 * @author yjd 12 * 13 */ 14 public class Book implements Comparable<Object> { // 定義名為Book的類,默認繼承自Object類 15 public int id;// 編號 16 public String name;// 名稱 17 public double price; // 價格 18 private String author;// 作者 19 public GregorianCalendar calendar;// 出版日期 20 21 public Book() { 22 this(0, "X", 0.0, new GregorianCalendar(), ""); 23 } 24 25 public Book(int id, String name, double price, GregorianCalendar calender, 26 String author) { 27 this.id = id; 28 this.name = name; 29 this.price = price; 30 this.calendar = calender; 31 this.author = author; 32 } 33 34 // 重寫繼承自父類Object的方法,滿足Book類信息描述的要求 35 public String toString() { 36 String showStr = id + "\t" + name; // 定義顯示類信息的字符串 37 DecimalFormat formatPrice = new DecimalFormat("0.00");// 格式化價格到小數點后兩位 38 showStr += "\t" + formatPrice.format(price);// 格式化價格 39 showStr += "\t" + author; 40 SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日"); 41 showStr += "\t" + formatDate.format(calendar.getTime()); // 格式化時間 42 return showStr; // 返回類信息字符串 43 } 44 45 public int compareTo(Object obj) {// Comparable接口中的方法 46 Book b = (Book) obj; 47 return this.id - b.id; // 按書的id比較大小,用於默認排序 48 } 49 50 public static void main(String[] args) { 51 Book b1 = new Book(10000, "紅樓夢", 150.86, new GregorianCalendar(2009, 52 01, 25), "曹雪芹、高鄂"); 53 Book b2 = new Book(10001, "三國演義", 99.68, new GregorianCalendar(2008, 7, 54 8), "羅貫中 "); 55 Book b3 = new Book(10002, "水滸傳", 100.8, new GregorianCalendar(2009, 6, 56 28), "施耐庵 "); 57 Book b4 = new Book(10003, "西游記", 120.8, new GregorianCalendar(2011, 6, 58 8), "吳承恩"); 59 Book b5 = new Book(10004, "天龍八部", 10.4, new GregorianCalendar(2011, 9, 60 23), "搜狐"); 61 TreeMap tm = new TreeMap(); 62 tm.put(b1, new Integer(255)); 63 tm.put(b2, new Integer(122)); 64 tm.put(b3, new Integer(688)); 65 tm.put(b4, new Integer(453)); 66 tm.put(b5, new Integer(40)); 67 Iterator it = tm.keySet().iterator(); 68 Object key = null, value = null; 69 Book bb = null; 70 while (it.hasNext()) { 71 key = it.next(); 72 bb = (Book) key; 73 value = tm.get(key); 74 System.out.println(bb.toString() + "\t庫存:" + tm.get(key)); 75 } 76 } 77 }
book類測試結果:
10000 紅樓夢 150.86 曹雪芹、高鄂 2009年02月25日 庫存:255
10001 三國演義 99.68 羅貫中 2008年08月08日 庫存:122
10002 水滸傳 100.80 施耐庵 2009年07月28日 庫存:688
10003 西游記 120.80 吳承恩 2011年07月08日 庫存:453
10004 天龍八部 10.40 搜狐 2011年10月23日 庫存:40