js实现HashMap功能代码

/***
 * js 实现hashmap  功能  
 * 常用功能实现
 * @return
 */

function HashMap(){
	this.length = 0;
	this.maxLength = Number.MAX_VALUE;
	this.container = {};
}

HashMap.prototype.put = function(objName,objValue){
	try{
		if(this.length >= this.maxLength)
			throw new Error("[Error HashMap] : Map Datas' count overflow !");
		if(objName != ""){
			for(var p in this.container){
				if(p == objName){
					this.container[objName] = objValue;
					return ;
				}
			}
			this.container[objName] = objValue;
			this.length ++ ;
		}
	}catch(e){
		return e;
	}
};

HashMap.prototype.get = function(objName){
	try{
		if(this.container[objName])
			return this.container[objName];
	}catch(e){
		return e;
	}
};

HashMap.prototype.contains = function(objValue){
	try{
		for(var p in this.container){
			if(this.container[p] === objValue)
				return true;
		}
		return false;
	}catch(e){
		return e;
	}
};

HashMap.prototype.remove = function(objName){
	try{
		if(this.container[objName]){
			delete this.container[objName];
			this.length -- ;
		}
	}catch(e){
		return e;
	}
};

HashMap.prototype.pop = function(objName){
	try{
		var ov = this.container[objName];
		if(ov){
			delete this.container[objName];
			this.length -- ;
			return ov;
		}
		return null;
	}catch(e){
		return e;
	}
};

HashMap.prototype.removeAll = function(){
	try{
		this.clear();
	}catch(e){
		return e;	
	}
};

HashMap.prototype.clear = function(){
	try{
		delete this.container;
		this.container = {};
		this.length = 0;
	}catch(e){
		return e;
	}
};

HashMap.prototype.isEmpty = function(){
	if(this.length === 0)
		return true;
	else
		return false;
};

HashMap.prototype.runIn = function(fun){
	try{
		if(!fun)
			throw new Error("[Error HashMap] : The paramer is null !");
		for(var p in this.container){
			var ov = this.container[p];
			fun(ov);
		}
	}catch(e){
		return e;
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值