TreeMap的基本用法(一)

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)));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值