java学习(130):treemap类
生活随笔
收集整理的這篇文章主要介紹了
java学习(130):treemap类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class Department {private String code;//部門編號private String name;//部門名字private int quanity;//部門人員數量public void setCode(String code) {this.code = code;}public String getCode() {return code;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setQuanity(int quanity) {this.quanity = quanity;}public int getQuanity() {return quanity;}
}
測試類
//treemap實現類 import java.util.Collection; import java.util.Comparator; import java.util.TreeMap;public class test69 {public static void main(String[] args){TreeMap depTree;depTree = new TreeMap(new Comparator() {@Overridepublic int compare(Objectk1, Objectk2) {Integer intk1 = (Integer) k1;Integer intk2 = (Integer) k2;if (intk1.intValue() > intk2.intValue()) {return -1;} else if (intk1.intValue() < intk2.intValue())return 1;return 0;}});Department dep0=new Department();dep0.setCode("dep400");dep0.setName("研發部");dep0.setQuanity(50);Department dep1=new Department();dep1.setCode("dep200");dep1.setName("人事部");dep1.setQuanity(30);Department dep2=new Department();dep2.setCode("dep100");dep2.setName("事業部");dep2.setQuanity(20);Department dep3=new Department();dep3.setCode("dep100");dep3.setName("市場部");dep3.setQuanity(100);//存儲數據depTree.put(new Integer(100),dep3);depTree.put(new Integer(50),dep0);depTree.put(new Integer(80),dep1);depTree.put(new Integer(10),dep2);Collection coll=depTree.values();for(Object o:coll){Department depTemp=(Department)o;System.out.println(depTemp.getName()+"\t"+depTemp.getCode());}} }運行結果
總結
以上是生活随笔為你收集整理的java学习(130):treemap类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 嵌入式C语言基础链表
- 下一篇: riso1855使用说明_理想CV185