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());}}
}
运行结果