给你个例子:
public class TestTreeMap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeMap<Amap,String> tm=new TreeMap<Amap,String>();
tm.put(new Amap(3),"轻量级应用");
tm.put(new Amap(-5),"Struts2权威指南");
tm.put(new Amap(9),"java疯狂 讲义");
System.out.println(tm);
}
}
class Amap implements Comparable<Amap>{
int count;
public String toString(){
return "A(count属性:"+count+
")";
}
public int compareTo(Amap o) {
// TODO Auto-generated method stub
if(this.count>o.count){
return 1;
}else if(this.count<o.count){
return -1;
}
return 0;
}
public Amap(int count) {
super();
this.count = count;
}
}
好好看看, |