数组序列中的元素: 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日 库存:25510001 三国演义 99.68 罗贯中 2008年08月08日 库存:12210002 水浒传 100.80 施耐庵 2009年07月28日 库存:68810003 西游记 120.80 吴承恩 2011年07月08日 库存:45310004 天龙八部 10.40 搜狐 2011年10月23日 库存:40
转载于:https://www.cnblogs.com/Lxiaojiang/p/6805151.html
