javascript版的HashMap


function HashMap() {
  var length = 0;
  var obj = new Object();
  this.isEmpty = function () {
    return length == 0;
  };
  this.containsKey = function (key) {
    return (key) ? (key in obj)  : false;
  };
  this.containsValue = function (value) {
    for (key in obj) {
      if (obj[key] == value) {
        return true;
      }
    }
    return false;
  };
  this.get = function (key) {
    return (this.containsKey(key)) ? obj[key] : null;
  };
  this.remove = function (key) {
    if (this.containsKey(key) && delete obj[key]) {
      length--;
    }
  };
  this.put = function (key, value) {
    if (!this.containsKey(key)) {
      length++;
    }
    obj[key] = value;
  };
  this.values = function () {
    var _values = new Array();
    for (key in obj) {
      _values.push(obj[key]);
    }
    return _values;
  };
  this.keySet = function () {
    var ks = new Array();
    for (key in obj) {
      ks.push(key);
    }
    return ks;
  };
  this.size = function () {
    return this.length;
  };
  this.clear = function () {
    this.length = 0;
    this.obj = new Object();
  };
  this.toJSON = function () {
    return JSON.stringify(obj,null,2);
  }
};

使用方法如下:

      var map = new HashMap();
      map.put(“key”, 1);
      map.remove("key");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM