亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

javascript實現java中的map

系統 2536 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條評論
主站蜘蛛池模板: 日本亚洲一区二区三区 | 欧美特级毛片a够爽 | 天天干天天夜 | 精品99牛牛视频 | 天天综合天天操 | 狠狠色狠狠色综合婷婷tag | 久久天天躁狠狠躁夜夜爽蜜月 | 日本高清视频一区二区三区 | 97视频免费公开成人福利 | 亚洲九九爱 | 国产精品免费观看网站 | 香蕉精品高清在线观看视频 | 欧美成人另类 | 免费一级毛片在线播放 | 99热国产这里只有精品99 | 国产精品日韩欧美一区二区 | 欧美a在线 | 精品国产一区二区 | 国产精品www| 奇米77777| 一级骚片 | 久久亚洲一级α片 | 一级黄片一级毛片 | 天天翘夜夜洗澡天天做 | 尤物视频在线免费观看 | 日本欧美一区二区三区在线观看 | 国产成人高清 | 国产精品毛片久久久久久久 | 香蕉福利 | 黑人日美女 | 久久2| 色九九视频 | 中国欧美日韩一区二区三区 | 久视频免费精品6 | 亚洲最大在线观看 | 日本欧美黄色 | 五月婷在线 | 色婷婷视频在线 | 国产免费一区二区三区在线 | 中文字幕不卡一区2021 | 日韩一区二区三区四区 |