javascript之HashMap

用法

var map = new HashMap();
					
					map.put("a",1);
					map.put("b",1);
					map.put("b",1);
					
					alert(map.get("a"));
					alert(map.keys().toString());
					alert(map.values.toString());
					alert(map.size());
					
					map.remove("a");
					
					alert(map.keys().toString());

代码

/*
jquery Map 对象
@desc:基于js的hashmap
 */
function HashMap() {
	/** Map 大小 * */
	var size = 0;
	/** 对象 * */
	var entry = new Object();
	/** 存 * */
	this.put = function(key, value) {
		if (!this.containsKey(key)) {
			size++;
		}
		entry[key] = value;
	}

	/** 取 * */
	this.get = function(key) {
		if (this.containsKey(key)) {
			return entry[key];
		} else {
			return null;
		}
	}

	/** 删除 * */
	this.remove = function(key) {

		if (delete entry[key]) {
			size--;
		}
	}

	/** 是否包含 Key * */
	this.containsKey = function(key) {
		return (key in entry);
	}

	/** 是否包含 Value * */
	this.containsValue = function(value) {
		for ( var prop in entry) {
			if (entry[prop] == value) {
				return true;
			}
		}
		return false;
	}

	/** 所有 Value * */
	this.values = function() {
		var values = new Array();
		for ( var prop in entry) {
			values.push(ent);
		}
		return values;
	}

	/** 所有 Key * */
	this.keys = function() {
		var keys = new Array();
		for ( var prop in entry) {
			var ent = entry[prop];
			if (ent != null && ent != undefined && ent.length > 0)
				keys.push(ent);
		}
		return keys;
	}

	/** Map Size * */
	this.size = function() {
		return size;
	}

	/** **移除map所有信息** */
	this.removeALl = function() {
		var str_key = this.keys();
		for (var i = 0; i < str_key.length; i++) {
			if (null != str_key[i] && "" != str_key[i]) {
				this.remove(str_key[i]);
			}
		}
	}
}

/**
 * 
 * 字符串拼接 QUINN
 * 
 */
function StringBuffer() {
	this._strs = new Array();
}
StringBuffer.prototype.append = function(str) {
	this._strs.push(str);
};
StringBuffer.prototype.arrayToString = function() {
	return this._strs.join("");
};

相关推荐