import java.util.*;
class R implements Comparable
{
int count;
public R(int count)
{
this.count=count;
}
public String toString()
{
return "R(count属性:"+count+")";
}
public boolean equals(Object obj)
{
if(this==obj)
{
return true;
}
if(obj!=null && obj.getClass()==R.class)
{
R r=(R) obj;
if(r.count==this.count)
{
return true;
}
}
return false;
}
public int compareTo(Object obj)
{
R r=(R) obj;
if(this.count>r.count)
{
return 1;
}
else if(this.count==r.count)
{
return 0;
}
return -1;
}
}
public class TestTreeMap
{
public static void main(String[] args)
{
TreeMap tm=new TreeMap();
tm.put(new R(3),"轻量级J2EE企业应用实战");
tm.put(new R(-5),"Struts2权威指南");
tm.put(new R(9),"ROR敏捷开发最佳实践");
System.out.println(tm);
System.out.println(tm.firstEntry());
System.out.println(tm.lastKey());
System.out.println(tm.higherKey(new R(2)));
System.out.println(tm.lowerEntry(new R(2)));
System.out.println(tm.subMap(new R(-1),new R(4)));
}
}
TreeMap的基本用法(一)
最新推荐文章于 2024-08-21 00:48:15 发布