JS Implementation Hashtable

Source: Internet
Author: User
Tags hasownproperty

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.