javascript hashtable实现代码

2009-10-13 45

var arr = new Array(); 
arr['item1'] = 'the value of item 1 '; 
arr['item2'] = 'the value of item 2 '; 
alert(arr['item1']); 
alert(arr['item2']);

但上述功能,不符我们的实际要求,另外查询遍历也不方便,我们需要在Array的基础上进行扩展,
下面我们可以用js中的数组来实现类似的hashtable的功能,
function Hashtable(){ 
this.clear = hashtable_clear; 
this.containsKey = hashtable_containsKey; 
this.containsValue = hashtable_containsValue; 
this.get = hashtable_get; 
this.isEmpty = hashtable_isEmpty; 
this.keys = hashtable_keys; 
this.put = hashtable_put; 
this.remove = hashtable_remove; 
this.size = hashtable_size; 
this.toString = hashtable_toString; 
this.values = hashtable_values; 
this.hashtable = new Array(); 
} 
function hashtable_clear(){ 
this.hashtable = new Array(); 
} 
function hashtable_containsKey(key){ 
var exists = false; 
for (var i in this.hashtable) { 
if (i == key && this.hashtable[i] != null) { 
exists = true; 
break; 
} 
} 
return exists; 
} 
function hashtable_containsValue(value){ 
var contains = false; 
if (value != null) { 
for (var i in this.hashtable) { 
if (this.hashtable[i] == value) { 
contains = true; 
break; 
} 
} 
} 
return contains; 
} 
function hashtable_get(key){ 
return this.hashtable[key]; 
} 
function hashtable_isEmpty(){ 
return (this.size == 0) ? true : false; 
} 
function hashtable_keys(){ 
var keys = new Array(); 
for (var i in this.hashtable) { 
if (this.hashtable[i] != null) 
keys.push(i); 
} 
return keys; 
} 
function hashtable_put(key, value){ 
if (key == null || value == null) { 
throw 'NullPointerException {' + key + '},{' + value + '}'; 
}else{ 
this.hashtable[key] = value; 
} 
} 
function hashtable_remove(key){ 
var rtn = this.hashtable[key]; 
//this.hashtable[key] =null; 
this.hashtable.splice(key,1); 
return rtn; 
} 
function hashtable_size(){ 
var size = 0; 
for (var i in this.hashtable) { 
if (this.hashtable[i] != null) 
size ++; 
} 
return size; 
} 
function hashtable_toString(){ 
var result = ''; 
for (var i in this.hashtable) 
{ 
if (this.hashtable[i] != null) 
result += '{' + i + '},{' + this.hashtable[i] + '}\n'; 
} 
return result; 
} 
function hashtable_values(){ 
var values = new Array(); 
for (var i in this.hashtable) { 
if (this.hashtable[i] != null) 
values.push(this.hashtable[i]); 
} 
return values; 
}

Hastable类使用方法:
//实例化一个自定义的哈希表类 
var hashTable = new Hashtable(); 
hashTable.put(0,'abc'); //0为key, 'abc'为value 
hashTable.put(1,'123'); 
hashTable.put(2,'88a'); 
hashTable.put(3,'88a'); 
//遍历hashtable, 相当于c#和java中的foreach 
for (var key in hashTable.keys()){ /* 用keys方法 */ 
alert(hashTable.get(key)); //按key遍历value 
} 
//遍历hashtable, 相当于c#和java中的foreach 
for (var key in hashTable.hashtable)){ /* 用hashtable属性 */ 
alert(hashTable.get(key)); //按key遍历value 
} 
alert(hashTable.containsKey(1)); //返回true 
alert(hashTable.containsKey(4)); //因为不存在key为4的,返回false 
alert(hashTable.containsValue('888')); //返回true 
alert(hashTable.containsValue('mobidogs')); //因为不存在value为'mobidogs'的,返回false 
hashTable.remove(1); //移除key为1的元素 
alert(hashTable.containsKey(1)); //因为key为1的元素已被上行的reomve()方法移除,所以返回false 
//关于hastable的其它方法使用简单,读者可以自己测试 (此略)
展开阅读全文

更多Javascript文章

javascript开发技术大全 第4章 直接量与字符集
Jul 03 44
Javascript中arguments对象详解
Oct 22 49
微信小程序通过api接口将json数据展现到小程序示例
Jan 20 40
vue学习笔记之vue1.0和vue2.0的区别介绍
May 17 36
jquery animate动画持续运动的实例
Nov 29 48
vue .js绑定checkbox并获取、改变选中状态的实例
Aug 24 53
js实现下拉框二级联动
Dec 04 42
手机访问当前页面