使用javascript模擬了一個類似Java的HashMap類

導讀:
  * 模擬簡單HashMap類,要求key是String,value可以是任何類型
  * 方法列表:
  * 1,HashMap() : 構造函數
  * 2, put(key, value) : void
  * 3, get(key) : Object
  * 4, keySet() : Array
  * 5, values() : Array
  * 6, size() : int
  * 7, clear() : void
  * 8, isEmpty() : boolean
  * 9, containsKey(key) : boolean
  * 10, containsValue(value) : boolean //如果value是Object類型或者數組類型,存在bug
  * 11, putAll(map) : void
  * 12, remove(key) : void
  * 用法:
  * var map = new JHashMap();
  * map.put("one","一隻小豬");
  * map.put("two","兩隻小豬");
  * map.put("three","三隻小豬");
  *
  * print("[ toString() ] : "+ map);
  * print("[ get() ] : "+ map.get("one"));
  * print("[ keySet() ] : "+ map.keySet());
  * print("[ values() ] : "+ map.values());
  * print("[ size() ] : "+ map.size());
  * print("[ isEmpty() ] : "+ map.isEmpty());
  * print("[ containsKey() ] : "+ map.containsKey("one"));
  * print("[ containsValue() ] : "+ map.containsValue("三隻小豬"));
  *
  * //putAll
  * var mapTemp = new JHashMap();
  * mapTemp.put("four","四隻小豬");
  * mapTemp.put("five","五隻小豬");
  * map.putAll(mapTemp);
  * print("[ putAll() ] : "+ map);
  *
  * //remove
  * map.remove("two");
  * map.remove("one");
  * print("[ remove() ] : "+ map);
  *
  * //clear
  * map.clear()
  * print("[ clear() ] : "+ map.size());
  * //輔助方法
  * function print(msg)
  * {
  * document.write(msg + "");
  * }
  * 修改履歷:
  -------------------------------------------------------------------------------*/





<SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript> <br></SCRIPT>
<IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-8432098110340913&dt=1207067143875&lmt=1207067863&prev_slotnames=6921970567&output=html&slotname=3493930840&correlator=1207067143812&url=http%3A%2F%2Fwww.busfly.cn%2FCSDN%2Fpost%2F535.html&ref=http%3A%2F%2Fwww.busfly.cn%2FCSDN%2Fpost%2FHashtable-HashMap.html&frm=0&cc=100&ga_vid=480888574.1207067144&ga_sid=1207067144&ga_hid=584304436&flash=9.0.115.0&u_h=800&u_w=1280&u_ah=770&u_aw=1280&u_cd=32&u_tz=480&u_his=2&u_java=true" frameBorder=0 width=250 scrolling=no height=250 allowTransparency></IFRAME>

  /**
  * HashMap構造函數
  */
  function JHashMap()
  {
  this.length = 0;
  this.prefix = "hashmap_prefix_20040716_";
  }
  /**
  * 向HashMap中添加鍵值對
  */
  JHashMap.prototype.put = function (key, value)
  {
  this[this.prefix + key] = value;
  this.length ++;
  }
  /**
  * 從HashMap中獲取value值
  */
  JHashMap.prototype.get = function(key)
  {
  return typeof this[this.prefix + key] == "undefined"
   null : this[this.prefix + key];
  }
  /**
  * 從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)
  str += strKey.substring(this.prefix.length)
  + ": "+ this[strKey] + "/r/n";
  }
  return str;
  }
  
  你相當於是用對象的屬性可增可減來模擬HashMap的key和value的集合的。
  但你有一點點忽視的問題。當
  頁面定義了Object.prototype時,你在for( var p in this )時會將Object.prototype也納入其中。
  如果是我設計,我會做成:
  function HashMap()
  {
  this.key = [];
  this.value = [];
  }做成2個集合的形式。

本文轉自
http://www.busfly.cn/CSDN/post/535.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章