java130-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() {
@Override
public int compare(Object
k1, Object
k2) {
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());
}
}
}
运行结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南