自己用Javascript写了一个Map,没有别人写的好,不过也要贴出来,作为学习参考!
function Map()
{
this.Entry = function(key,value){
this.key=key;
this.value=value;
};
this.EntrySet = new Array();
this.constraint = function(key){
if(null==key || undefined==key ||"undefined"==typeof(key) || ""==key)
{
return false;
}
return true;
};
this.entry = function (key){
if(!this.constraint(key))return;
for(var i=0; i<this.EntrySet.length; i++)
{
var entry = this.EntrySet[i];
if(entry.key==key) {
return entry;
};
}
return;
};
this.put=function(key,value){
if(!this.constraint(key))return;
var entry = this.entry(key);
if(entry)
{
entry.value = value;
}else
{
this.EntrySet.push(new this.Entry(key,value));
}
};
this.get=function(key){
var entry = this.entry(key);
if(entry)
{
return entry.value;
}
return;
};
}//end of map define
下面把人家写的HashMap也贴出来,供参考学习!
/**
* HashMap构造函数
*/
function HashMap()
{
this.length = 0;
this.prefix = "hashmap_prefix_20040716_";
}
/**
* 向HashMap中添加键值对
*/
HashMap.prototype.put = function (key, value)
{
if(!this[this.prefix+key]){
this[this.prefix + key] = value;
this.length ++;
}
}
/**
* 从HashMap中获取value值
*/
HashMap.prototype.get = function(key)
{
return typeof this[this.prefix + key] == "undefined"
? null : this[this.prefix + key];
}
/**
* 从HashMap中获取所有key的集合,以数组形式返回
*/
HashMap.prototype.keySet = function()
{
var arrKeySet = new Array();
var index = 0;
for(var strKey in this)
{
if(strKey.substring(0,this.prefix.length) == this.prefix)
arrKeySet[index ++] = strKey.substring(this.prefix.length);
}
return arrKeySet.length == 0 ? null : arrKeySet;
}
/**
* 从HashMap中获取value的集合,以数组形式返回
*/
HashMap.prototype.values = function()
{
var arrValues = new Array();
var index = 0;
for(var strKey in this)
{
if(strKey.substring(0,this.prefix.length) == this.prefix)
{
arrValues[index ++] = this[strKey];
}
}
return arrValues.length == 0 ? null : arrValues;
}
/**
* 获取HashMap的value值数量
*/
HashMap.prototype.size = function()
{
return this.length;
}
/**
* 删除指定的值
*/
HashMap.prototype.remove = function(key)
{
delete this[this.prefix + key];
this.length --;
}
/**
* 清空HashMap
*/
HashMap.prototype.clear = function()
{
for(var strKey in this)
{
if(strKey.substring(0,this.prefix.length) == this.prefix)
delete this[strKey];
}
this.length = 0;
}
/**
* 判断HashMap是否为空
*/
HashMap.prototype.isEmpty = function()
{
return this.length == 0;
}
/**
* 判断HashMap是否存在某个key
*/
HashMap.prototype.containsKey = function(key)
{
for(var strKey in this)
{
if(strKey == this.prefix + key)
return true;
}
return false;
}
/**
* 判断HashMap是否存在某个value
*/
HashMap.prototype.containsValue = function(value)
{
for(var strKey in this)
{
if(this[strKey] == value)
return true;
}
return false;
}
/**
* 把一个HashMap的值加入到另一个HashMap中,参数必须是HashMap
*/
HashMap.prototype.putAll = function(map)
{
if(map == null)
return;
if(map.constructor != HashMap)
return;
var arrKey = map.keySet();
var arrValue = map.values();
for(var i in arrKey)
this.put(arrKey[i],arrValue[i]);
}
//toString
HashMap.prototype.toString = function()
{
var str = "";
for(var strKey in this)
{
if(strKey.substring(0,this.prefix.length) == this.prefix)
str += strKey.substring(this.prefix.length)
+ " : " + this[strKey] + "\r\n";
}
return str;
}
本文分享了两种使用JavaScript实现Map的方法:一种是通过数组存储键值对;另一种是利用对象属性模拟HashMap,后者还提供了丰富的API如get、put、remove等。
1116

被折叠的 条评论
为什么被折叠?



