Home  >  Article  >  Web Front-end  >  javascript hashtable implementation code_javascript skills

javascript hashtable implementation code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:44:591246browse
Copy code The code is as follows:

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

But the above functions do not meet our actual requirements. In addition, query traversal is inconvenient. We need to expand on the basis of Array.
Below we can use js Array to implement similar hashtable functions,
Copy code The code is as follows:

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;
}

How to use Hastable class:
Copy code The code is as follows:

/ /Instantiate a custom hash table class
var hashTable = new Hashtable();
hashTable.put(0,'abc'); //0 is key, 'abc' is value
hashTable.put(1,'123');
hashTable.put(2,'88a');
hashTable.put(3,'88a');
//Traverse hashtable, equivalent to c# and foreach in java
for (var key in hashTable.keys()){ /* Use keys method*/
alert(hashTable.get(key)); //Traverse value by key
}
//Traverse hashtable, equivalent to foreach in c# and java
for (var key in hashTable.hashtable)){ /* Use hashtable attributes*/
alert(hashTable.get(key)); //Traverse value by key
}
alert(hashTable.containsKey(1)); //Return true
alert(hashTable.containsKey(4)); //Because there is no key with key 4, Return false
alert(hashTable.containsValue('888')); //Return true
alert(hashTable.containsValue('mobidogs')); //Because there is no value with value 'mobidogs', return false
hashTable.remove(1); //Remove the element with key 1
alert(hashTable.containsKey(1)); //Because the element with key 1 has been removed by the upstream reomve() method , so return false
//Other methods about hastable are simple to use, readers can test them themselves (this is omitted)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn