What are Hashmaps and Hashtables in Javascript?

What are Hashmaps and Hashtables in Javascript?

In this blog, I will be talking about hashmaps, hashtable, and their implementation.

Table Of Content

  1. What is Hashmap?

  2. What is Hashtable?

  3. Implementation

What is HashMap

A hashmap is a data structure containing an unordered collection of keys that are mapped to values using hashing( representing it with a hash value ).

Because of their array-like format, hashmaps map key labels to corresponding array indices where values are stored.

This removes the limitation of sequential numerical indices for ordering data, in turn allowing the use of flexible keys instead!

image.png

What is Hash Table

Hashtable is a data structure that allows us to create a list of paired values [ i.e, key-value pairs ]. We can retrieve the value by using the key for that particular value that we want. A hash table transforms a key into an integer index using a hash function.

image.png

Implementation

  • In javascript hash tables are implemented through Objects and Map.
  • In object data type we can pair an object's property value with a property key.
  • Example:
    var testObject = {
    name: "Tester",
    work: "Programming"
    }
    
  • We can overwrite the default properties.
  • Map is the same just like as Objects but the main difference is we can't update default properties.
  • It requires set and get methods to set and get the values.
  • Example:
const collection = new Map();

collection.set("Kuldeep", "12345");
collection.set("Gupta", "6789");

console.log(collection.get("Kuldeep")); // 12345
console.log(collection.size); // 2