public class TreeMapDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<TreeElement,Integer> map = new TreeMap<TreeElement,Integer>();
map.put(new TreeElement("lisi",100), 1);
map.put(new TreeElement("ahangsan",100), 3);
map.put(new TreeElement("wangwu",100), 2);
map.put(new TreeElement("wangwu",101), 3);
map.put(new TreeElement("zhaoli",100), 4);
System.out.println(map.remove(new TreeElement("zhaoli",100)));
System.out.println(map.get(new TreeElement("wangwu",100)));
Set<TreeElement> set = map.keySet();
Iterator<TreeElement> it =set.iterator();
while(it.hasNext())
{
TreeElement te = it.next();
Integer value = map.get(te);
System.out.println(te+"----"+value);
}
}
}
class TreeElement implements Comparable
{
String name;
public String getName() {
return name;
}
public int getId() {
return id;
}
int id;
TreeElement(String name,int id)
{
this.name = name ;
this.id = id;
}
@Override
public int compareTo(Object o) {
if(!(o instanceof TreeElement))
throw new RuntimeException("类型不匹配");
TreeElement tree = (TreeElement)o;
int num =this.name.compareTo(tree.name);
//System.out.println(this.name+"---compareTo---"+tree.name);
if(num==0)
{
return new Integer(this.id).compareTo(new Integer(tree.id));
}
return num;
}
public String toString()
{
return name+":"+id;
}
}
这样写看行不行
反正我是可以删除和获取 |