hashtable的javascript实现
类别:Web前端 浏览量:80
时间:2015-1-25 hashtable的javascript实现
hashtable的javascript实现javascript实现hashtable类似功能的代码
function HashTable() {
var size = 0;
var entry = new Object();
this.put = function (key, value) {
entry[key] = value;
size++;
};
this.putAll = function (htable) {
if (typeof htable == "object" && !htable.sort) {
for (var key in htable) {
this.put(key, htable[key]);
}
} else {
throw "输入类型不正确,必须是HashTable类型!";
}
};
this.get = function (key) {
return entry[key];
};
this.remove = function (key) {
if (size == 0)
return;
delete entry[key];
size--;
};
this.containsKey = function (key) {
if (entry[key]) {
return true;
}
return false;
};
this.containsValue = function (value) {
for (var key in entry) {
if (entry[key] == value) {
return true;
}
}
return false;
};
this.clear = function () {
entry = new Object();
size = 0;
};
this.isEmpty = function () {
return size == 0;
};
this.size = function () {
return size;
};
this.keySet = function () {
var keys = new Array();
for (var key in entry) {
keys.push(key);
}
return keys;
};
this.entrySet = function () {
var entrys = new Array();
for (var key in entry) {
var et = new Object();
et[key] = entry[key];
entrys.push(et);
}
return entrys;
};
this.values = function () {
var values = new Array();
for (var key in entry) {
values.push(entry[key]);
}
return values;
};
this.each = function (cb) {
for (var key in entry) {
cb.call(this, key, entry[key]);
}
};
this.toString = function () {
return obj2str(entry);
};
function obj2str(o) {
var r = [];
if (typeof o == "string")
return "\\"" + o.replace(/([\\'\\"\\\\])/g, "\\\\$1").replace(/(\\n)/g, "\\\\n").replace(/(\\r)/g, "\\\\r").replace(/(\\t)/g, "\\\\t") + "\\"";
if (typeof o == "object") {
for (var i in o)
r.push("\\"" + i + "\\":" + obj2str(o[i]));
if (!!document.all && !/^\\n?function\\s*toString\\(\\)\\s*\\{\\n?\\s*\\[native code\\]\\n?\\s*\\}\\n?\\s*$/.test(o.toString)) {
r.push("toString:" + o.toString.toString());
}
r = "{" + r.join() + "}";
return r;
}
return o.toString();
}
}
使用方法
var ht = new HashTable();
ht.put("key","value");
ht.size();
ht.toString();
ht.values();
ht.entrySet()
标签:hashtable
热门推荐
- python下划线怎么用(Python3中_下划线和__双下划线的用途和区别)
- 阿里云ecs所有文件备份到本地(阿里云ECS服务器的系统重装、快照备份、恢复数据回滚教程)
- linux系统登录mysql数据库(Linux手动部署远程的mysql数据库的方法详解)
- mysql哪些配置影响性能
- php用什么解压(php解压缩zip和rar压缩包文件的方法)
- laravel架构设置(Laravel框架查询构造器 CURD操作示例)
- mysql中行列转换(mysql 行列转换的示例代码)
- 安装apache服务器配置(Apache 配置详解最好的APACHE配置教程)
- sqlserver分页数据重复(SQL Server 在分页获取数据的同时获取到总记录数)
- .NET使用Html Agility Pack解析html