1. The hash table uses key-value pairs for data storage, to establish a one by one correspondence between where the data is stored and its keywords, so that the keyword and a unique storage location in the structure correspond, so when retrieving the data
This relationship allows you to quickly locate the data you are looking for.
function HashTable () {
this._hash={};
this._count=0;
Add or UPDATE key
This.put=function (Key,value) {
if (This._hash.hasownproperty (key)) {
This._hash[key]=value;
return true;
}
else{
This._hash[key]=value;
this._count++;
return true;
}
}
Gets the value specified by the key
This.get=function (key) {
if (This.containskey (key)) {
return This._hash[key];
}
}
Get the number of elements
This.size=function () {
return this._count;
}
Check whether it is empty
This.isempty=function () {
if (this._count<=0) return true;
else return false;
}
Checks whether the specified key is included
This.containskey=function (key) {
return This._hash.hasownproperty (key);
}
Checks whether the specified value is included
This.containsvalue=function (value) {
for (Var strkey in This._hash) {
if (This._hash[strkey]==value) {
return true;
}
}
return false;
}
Delete a key
This.remove=function (key) {
Delete This._hash[key];
this._count--;
}
Clear all the keys
This.clear=function () {
this._hash={};
this._count=0;
}
Gets the collection of keys from Hashtable, returned as an array
This.keyset=function () {
var arrkeyset = [];
var index=0;
for (Var strkey in This._hash) {
Arrkeyset[index++]=strkey;
}
Return (arrkeyset.length==0)? Null:arrkeyset;
}
Gets a collection of value from Hashtable, returned as an array
This.valueset=function () {
var arrvalues=[];
var index=0;
for (Var strkey in This._hash) {
Arrvalues[index++]=this._hash[strkey];
}
Return (arrvalues.length==0)? null:arrvalues;
}
}
Test Case:
Var ht =new HashTable ();
Ht.put ("Key", "value");
Ht.put ("Key2", "value2");
Alert (Ht.keyset ());
Alert (Ht.valueset ());
Alert (Ht.get ("key"));
Ht.remove ("key");
Alert (Ht.get ("key"));
Ht.clear ();