Javascript 中 HashMap 实现

    诸如HashMap、Stack、Queue都可以通过操作JS数组进行实现。下面我们首先参照Javascript官方使用手册对JS Array对象中的一些常用函数进行解释一下,以便进行下面的数据结构的实现(JS Array对象类似于堆栈的元素进出栈顺序):
  Push :  添加一个元素
  Reverse:将数组元素进行逆序
  Shift:移除数组中第一个元素(下标为0)  

  Pop:弹出最后一个元素(下标为array.length-1)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <script type="text/javascript">
   
  function HashMap(){
	 this.source = new Array();
	 this.length = 0; 
  };

   /*将函数列表(实为JSON数据)映射到HashMap的prototype属性中*/
  function Apply(o, c){      
		if(o && c && typeof c == 'object'){
			for(var p in c){
				o[p] = c[p];
			}
		}
		return o;
  };

 
 Apply(HashMap.prototype,{
   
	/*获取元素的个数*/
	size : function(){ return  this.length },
	
	/*查看是否存在该key值*/
	containsKey :function (key) {  
        if(this.source[key])
		  return true;
		else 
          return false;  
    }, 

    /*查看是否存在该value值*/
	containsValue :function (value) {  
        for (var key in this.source) {  
            if (this.source[key] == value) {  
                return true;  
            }  
        }  
        return false;  
    }, 
	
	/*存入数据  
	 返回值如果为false表示该key值已经存过数据,我们会替换前一条数据
	*/
	put : function(key,value)
	    {
		   var tag = this.containsKey(key);
		   this.length += ((tag)? 0: 1);
		   this.source[key] = value;
	       
		   return !tag; 
	    },
    
	/*根据key读取数据*/
    get :  function(key)
       {
		 return this.source[key];
       },
 
   
	 /* 根据key值删除HashMap中元素,返回删除元素的value */  
     remove : function(key)   
     {   
	     var value = this.source[key];
         if( this.containsKey(key) && ( delete this.source[key] ) )   
         {   
             this.length --;   
         }   
         return value;
     },


	 /*清除所有数据*/
	 clear : function()
	 {
	   delete this.source;
	   this.source = new Array();
	   this.length = 0;
	   return 0;
	 },

     
	 obj2str :function(o) {  
        var r = [];  
        if (typeof o == "string")  
            return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";  
        if (typeof o == "object") {  
            for (var i in o)  
                r.push("\"" + i + "\":" + this.obj2str(o[i]));  
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {  
                r.push("toString:" + o.toString.toString());  
            }  
            r = "{" + r.join() + "}";  
            return r;  
        }  
        return o.toString();  
     },

	 /*将数据以JSON字串形式输出*/
	 toString : function()
	 { 
	    return this.obj2str(this.source);
	 } 
 
 
 });
 
 var hsp = new HashMap();

 document.writeln(hsp.put('a',"apple"));   //true
 document.writeln(hsp.put('b',"boy"));     //true
 document.writeln(hsp.put('c',"cat"));    //true
 document.writeln(hsp.put('a',"replaceApple"));  //false
 document.writeln(hsp.get("a"));   //"replaceApple"
 document.writeln(hsp.size());     //3
 document.writeln(hsp.containsValue('cat'));  //true
 document.writeln(hsp.containsKey('a'));  //true
 document.writeln(hsp.remove('a'));    //"replaceApple"
 document.writeln(hsp.toString());   //"{"b":"boy","c":"cat"}"
 document.writeln(hsp.clear());    //2
 document.writeln(hsp.toString());   "{}"



 
 
 </script>
 </head>

 <body>
  
 </body>
</html>


注:Stack和Queue的实现可以参看: http://blog.csdn.net/wdxgdiy/article/details/8877565   写的不错

附件下载 :  http://pan.baidu.com/share/link?shareid=3844618167&uk=1763003608

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值