javascript实现java中的map

系统 1712 0

Map.js

     function Map(linkItems) {   
     this.current = undefined;   
     this._size = 0;   
     if(linkItems === false){  
         this.disableLinking();   
     }   
 }  
 /** 
  * 获取当前map 
  * @return 当前对象 
  */  
 Map.noop = function() {   
     return this;   
 };   
 /** 
  * 非法操作 
  * @return 
  */  
 Map.illegal = function() {   
     throw new Error("非法操作,Map已经被禁用");   
 };   
 /** 
  *  
  * @param obj 
  * @param foreignKeys 
  * @return 
  */  
 Map.from = function(obj, foreignKeys) {   
     var map = new Map;   
     for(var prop in obj) {   
         if(foreignKeys || obj.hasOwnProperty(prop)){  
             map.put(prop, obj[prop]);   
         }   
     }   
     return map;   
 };   
 /** 
  * 禁用map 
  * @return 
  */  
 Map.prototype.disableLinking = function() {   
     this.link = Map.noop;   
     this.unlink = Map.noop;   
     this.disableLinking = Map.noop;   
     this.next = Map.illegal;   
     this.key = Map.illegal;   
     this.value = Map.illegal;   
     this.clear = Map.illegal;   
     return this;   
 };   
 /** 
  * 返回hash值 例如:number 123 
  * @param value key/value 
  * @return 
  */  
 Map.prototype.hash = function(value) {   
     return (typeof value) + ' ' + (value instanceof Object ? (value.__hash || (value.__hash = ++arguments.callee.current)) : value.toString());   
 };   
 /** 
  * 返回map的长度 
  * @return 
  */  
 Map.prototype.size = function() {   
     return this._size;  
 };   
   
 Map.prototype.hash.current = 0;   
 /** 
  * 通过key获取value 
  * @param key 
  * @return 
  */  
 Map.prototype.get = function(key) {   
     var item = this[this.hash(key)];   
     return item === undefined ? undefined : item.value;   
 };   
 /** 
  * 向map中添加数据 
  * @param key 键 
  * @param value 值 
  * @return 
  */  
 Map.prototype.put = function(key, value) {   
     var hash = this.hash(key);   
     if(this[hash] === undefined) {   
         var item = { key : key, value : value };   
         this[hash] = item;   
         this.link(item);   
         ++this._size;   
     }else{  
         this[hash].value = value;  
     }   
     return this;   
 };   
 /** 
  * 通过key删除数据 
  * @param key 
  * @return 
  */  
 Map.prototype.remove = function(key) {   
     var hash = this.hash(key);   
     var item = this[hash];   
     if(item !== undefined) {   
         --this._size;   
         this.unlink(item);   
         delete this[hash];   
     }   
     return this;   
 };   
 /** 
  * 清除map 
  * @return 
  */  
 Map.prototype.clear = function() {   
     while(this._size){  
         this.remove(this.key());   
     }   
     return this;   
 };   
 /** 
  * 处理队列 
  * @param item 
  * @return 
  */  
 Map.prototype.link = function(item) {   
     if(this._size == 0) {   
         item.prev = item;   
         item.next = item;   
         this.current = item;   
     }else {   
         item.prev = this.current.prev;   
         item.prev.next = item;   
         item.next = this.current;   
         this.current.prev = item;  
     }   
 };   
 Map.prototype.unlink = function(item) {   
     if(this._size == 0){   
         this.current = undefined;  
     }else {   
         item.prev.next = item.next;   
         item.next.prev = item.prev;   
         if(item === this.current){  
             this.current = item.next;   
         }   
     }   
 };   
 /** 
  * 获取下一个 
  * @return 
  */  
 Map.prototype.next = function() {   
     this.current = this.current.next;   
     return this;  
 };   
 /** 
  * 获取key 
  * @return 
  */  
 Map.prototype.key = function() {   
     return this.current.key;   
 };   
 /** 
  * 获取value 
  * @return 
  */  
 Map.prototype.value = function() {   
     return this.current.value;   
 };
  

 测试代码如下:

     var l=10000;  
     var map=new Map();  
     var start=new Date().getTime();  
     for(var i=0;i<l;i++){  
         map.put("key_"+i,new Date());  
     }  
     var end=new Date().getTime();  
     document.write("向map中添加了  "+l+" 个Date对象..........");  
     document.write("<br/>");  
     document.write("耗时  "+(end-start)+" 毫秒,map的长度为:"+map.size());  
     document.write("<br/>");  
     document.write("在map中提取全部数据..........");  
     document.write("<br/>");  
     start=new Date().getTime();  
     for(var i=0;i<map.size();i++){  
         map.get("key_"+i).getTime();  
     }  
     end=new Date().getTime();  
     document.write("耗时  "+(end-start)+" 毫秒");  
     document.write("<br/>");  
     document.write("清空map..........");  
     document.write("<br/>");  
     start=new Date().getTime();  
     map.clear();  
     end=new Date().getTime();  
     document.write("耗时  "+(end-start)+" 毫秒,map的长度为:"+map.size());  
     document.write("<br/>"); 
  
  •  IE7

javascript实现java中的map

  • Firefox 3.6.8

javascript实现java中的map

  • 谷歌浏览器5.0

javascript实现java中的map

 

方法next的使用:

     var map=new Map();  
     map.put("key_1","value_1");  
     map.put("key_2","value_2");  
     map.put("key_3","value_3");  
     var m=map.next();  
     document.write("map.next:key="+m.key()+" value="+m.value());  
     document.write("<br/>");  
     m=map.next();  
     document.write("map.next:key="+m.key()+" value="+m.value());
  

 结果如下:

    map.next:key=key_2 value=value_2  
map.next:key=key_3 value=value_3
  
 

[ 转自 : http://dh189.iteye.com/blog/724632 ]

javascript实现java中的map


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论