首页 > 代码库 > TreeMap图书馆案例
TreeMap图书馆案例
package tushu; import java.util.Comparator; import java.util.TreeMap; public class TreeMapTest { public static void main(String[] args) { TreeMap<String,Book> map=new TreeMap<String,Book>(new MyComparator()); map.put("B45678", new Book("B45678","钢铁是怎样连城的","保尔",56)); map.put("B678", new Book("B678","世界时平的","保尔",56)); map.put("D89", new Book("D89","java编程思想","kobe",56)); map.put("A15678", new Book("A15678","高等数学","joms",56)); System.out.println(map); } } class Book { String bookno; String bookName; String author; int price; public String getBookno() { return bookno; } public void setBookno(String bookno) { this.bookno = bookno; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Book [bookno=" + bookno + ", bookName=" + bookName + ", author=" + author + ", price=" + price + "]"; } public Book(String bookno, String bookName, String author, int price) { super(); this.bookno = bookno; this.bookName = bookName; this.author = author; this.price = price; } } class MyComparator implements Comparator<String>{ @Override public int compare(String o1, String o2) { if(o1.length()==o2.length()){ return o1.compareTo(o2); } return o1.length()-o2.length(); } }
TreeMap图书馆案例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。