js实现java中HashMap的功能

                                                                                           javascript实现java中的HashMap的功能  

               代码如下:希望对你们有所帮助。

               

              /**
 * HashMap构造函数
 */
function JHashMap(){
    this.length = 0;
    this.prefix = "hashmap_prefix_20080716_";
}
/**
 * 向HashMap中添加键值对
 */
JHashMap.prototype.put = function (key, value){
if(this.get(key)==null) {
   this[this.prefix + key] = value;
   this.length++;
}
else {
this.replace(key, value);
}
}
/**
 * 从HashMap中获取value值
 */
JHashMap.prototype.get = function(key){
//alert("get:prefix>>:"+this.prefix + key+"::::::"+this[this.prefix + key]);
//return this[this.prefix + key];
    return (typeof this[this.prefix + key] == "undefined")?null : this[this.prefix + key];
}
/**
 * 替換HashMap中對應key的值
 */
JHashMap.prototype.replace = function (key, value){
    this.remove(key);
    this.put(key,value);
}
/**
 * 从HashMap中获取所有key的集合,以数组形式返回
 */
JHashMap.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的集合,以数组形式返回
 */
JHashMap.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值数量
 */
JHashMap.prototype.size = function(){
    return this.length;
}
/**
 * 删除指定的值
 */
JHashMap.prototype.remove = function(key){
    delete this[this.prefix + key];
    this.length--;
}
/**
 * 清空HashMap
 */
JHashMap.prototype.clear = function(){
    for(var strKey in this){
        if(strKey.substring(0,this.prefix.length) == this.prefix)
            delete this[strKey];   
    }
    this.length = 0;
}
/**
 * 判断HashMap是否为空
 */
JHashMap.prototype.isEmpty = function(){
    return this.length == 0;
}
/**
 * 判断HashMap是否存在某个key
 */
JHashMap.prototype.containsKey = function(key){
    for(var strKey in this){
       if(strKey == this.prefix + key)
          return true;  
    }
    return false;
}
/**
 * 判断HashMap是否存在某个value
 */
JHashMap.prototype.containsValue = function(value){
    for(var strKey in this){
       if(this[strKey] == value)
          return true;  
    }
    return false;
}
/**
 * 把一个HashMap的值加入到另一个HashMap中,参数必须是HashMap
 */
JHashMap.prototype.putAll = function(map){
    if(map == null)
        return;
    if(map.constructor != JHashMap)
        return;
    var arrKey = map.keySet();
    var arrValue = map.values();
    for(var i in arrKey)
       this.put(arrKey[i],arrValue[i]);
}
//toString
JHashMap.prototype.toString = function(){
    var str = "";
    for(var strKey in this){
        if(strKey.substring(0,this.prefix.length) == this.prefix) {
          if(str=="")
               str = strKey.substring(this.prefix.length) + "=" + this[strKey] ;
              else
               str +=  "|"+ strKey.substring(this.prefix.length) + "=" + this[strKey];
         }
    }
    return str;
}

           谨记大家js代码一定要写的规范,方便人去阅读你的代码,要好好养成习惯.


 




             

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值