Implementation of a hash table (hashtable) in Javascript

Source: Internet
Author: User
Hashtable implementation in Javascript

JavaScript does not have a hash table (hashtable) like C # or Java ),
However, arrays in Javascript only have simple functions similar to 'hash table', as shown below:

  1. VaR arr = new array ();
  2. Arr ['item1'] = 'the value of item 1 ';
  3. Arr ['item2'] = 'the value of item 2 ';
  4. Alert (ARR ['item1']);
  5. Alert (ARR ['item2']);

However, the above functions do not meet our actual requirements. In addition, query traversal is not convenient. We need to expand them on the basis of array,
In the following example, we can use arrays in JS to implement hashtable functions,

  1. Function hashtable (){
  2. This. Clear = hashtable_clear;
  3. This. containskey = hashtable_containskey;
  4. This. containsvalue = hashtable_containsvalue;
  5. This. Get = hashtable_get;
  6. This. isempty = hashtable_isempty;
  7. This. Keys = hashtable_keys;
  8. This. Put = hashtable_put;
  9. This. Remove = hashtable_remove;
  10. This. size = hashtable_size;
  11. This. tostring = hashtable_tostring;
  12. This. Values = hashtable_values;
  13. This. hashtable = new array ();
  14. }
  15. Function hashtable_clear (){
  16. This. hashtable = new array ();
  17. }
  18. Function hashtable_containskey (key ){
  19. VaR exists = false;
  20. For (var I in this. hashtable ){
  21. If (I = Key & this. hashtable [I]! = NULL ){
  22. Exists = true;
  23. Break;
  24. }
  25. }
  26. Return exists;
  27. }
  28. Function hashtable_containsvalue (value ){
  29. VaR contains = false;
  30. If (value! = NULL ){
  31. For (var I in this. hashtable ){
  32. If (this. hashtable [I] = value ){
  33. Contains = true;
  34. Break;
  35. }
  36. }
  37. }
  38. Return contains;
  39. }
  40. Function hashtable_get (key ){
  41. Return this. hashtable [Key];
  42. }
  43. Function hashtable_isempty (){
  44. Return (this. size = 0 )? True: false;
  45. }
  46. Function hashtable_keys (){
  47. VaR keys = new array ();
  48. For (var I in this. hashtable ){
  49. If (this. hashtable [I]! = NULL)
  50. Keys. Push (I );
  51. }
  52. Return keys;
  53. }
  54. Function hashtable_put (Key, value ){
  55. If (Key = NULL | value = NULL ){
  56. Throw 'nullpointerexception {'+ key +'}, {'+ value + '}';
  57. } Else {
  58. This. hashtable [Key] = value;
  59. }
  60. }
  61. Function hashtable_remove (key ){
  62. VaR RTN = This. hashtable [Key];
  63. // This. hashtable [Key] = NULL;
  64. This. Hashtable.Splice(Key, 1 );
  65. Return RTN;
  66. }
  67. Function hashtable_size (){
  68. VaR size = 0;
  69. For (var I in this. hashtable ){
  70. If (this. hashtable [I]! = NULL)
  71. Size ++;
  72. }
  73. Return size;
  74. }
  75. Function hashtable_tostring (){
  76. VaR result = '';
  77. For (var I in this. hashtable)
  78. {
  79. If (this. hashtable [I]! = NULL)
  80. Result + = '{' + I + '}, {' + this. hashtable [I] + '}/N ';
  81. }
  82. Return result;
  83. }
  84. Function hashtable_values (){
  85. VaR values = new array ();
  86. For (var I in this. hashtable ){
  87. If (this. hashtable [I]! = NULL)
  88. Values. Push (this. hashtable [I]);
  89. }
  90. Return values;
  91. }

Hastable class usage:

  1. // Instantiate a custom hash table class
  2. VaR hashtable = new hashtable ();

  3. Hashtable. Put (0, 'abc'); // 0 indicates the key and 'abc' indicates the value.
  4. Hashtable. Put (1, '123 ');
  5. Hashtable. Put (2, '88a ');
  6. Hashtable. Put (3, '88a ');

  7. // Traverse hashtable, equivalent to foreach in C # and Java
  8. For (var key in hashtable.Keys() {/* Use the keys Method */
  9. Alert (hashtable.Get(Key); // traverse value by key
  10. }

  11. // Traverse hashtable, equivalent to foreach in C # and Java
  12. For (var key in hashtable. hashtable) {/* use the hashtable attribute */
  13. Alert (hashtable.Get(Key); // traverse value by key
  14. }

  15. Alert (hashtable.Containskey(1); // return true
  16. Alert (hashtable.Containskey(4); // if the key is 4 because it does not exist, false is returned.

  17. Alert (hashtable.Containsvalue('20140901'); // return true
  18. Alert (hashtable.Containsvalue('Doogs'); // returns false because the value of 'mobiogs' does not exist.

  19. Hashtable.Remove(1); // remove the element whose key is 1
  20. Alert (hashtable.Containskey(1); // The element whose key is 1 has been removed by the upstream reomve () method, so false is returned.

  1. // Other hastable methods are easy to use. You can test them by yourself)

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.