How to use Hashmap in TypeScript?


The Hashmap is the one kind of data structure that stores the key-value pairs of the different data.  Like other programming languages, TypeScript also contains a built-in map data structure. In JavaScript, we can't define the key or value type that needs to be stored in the map. So, we need to create a map of the generic type. In TypeScript, we can define the type of the key and value to be stored in the map.

Syntax

Following is the syntax create the map in TypeScript −

let hashMap = new Map<Key_Type, value_Type>();

Parameters

  • key_Type − It is a data type of the key. Users can use String, numbers, boolean, Object, Array, etc.

  • value_Type − It is a data type of the value.

Hashmap Methods in TypeScript

The map class of the TypeScript contains the 5 to 6 methods that we can invoke by taking any object of the map class as a reference. Users can see the explanation for every method below.

  • get(key) − The get() method of the map class is useful to get the value stored in the map for the particular key. The hashmap contains the unique keys, and we can access its value using the get() method.

let hashMap = new Map<number, string>();
hashmap.get(1);
  • set(key, value) − The set() method of the map class allows us to set the key-value pair in the map. It takes the key as the first parameter and the value as the second parameter. Also, it takes a unique key. If the user passes the existing key, the set() method replaces the existing key's value.

let hashMap = new Map<number, string>();
hashMap.set(1,"TutorialsPoint");
  • delete(key) − The delete() method takes the key as a parameter and deletes the key and its mapped value from the map object.

let hashMap = new Map<number, string>();
hashMap.delete(1);
  • has(key) − The has() method also takes the key as a parameter and checks whether the hashmap contains the particular key and its mapped value.

let hashMap = new Map<number, string>();
hashMap.has(1);
  • Size − The size is a variable of the map class contains the numeric value representing the total number of key-value pair hashmap contains.

let hashMap = new Map<number, string>();
let total = hashMap.size;
  • clear() − The clear() method removes the all key-value pairs from the map() object and makes its size to 0.

let hashMap = new Map<number, string>();
hashMap.clear();

Example

In the example below, we have created a map and stored it in the “hashmap”. After that, we used the set() method to set the 7 different key-value pairs in the hashmap. Next, we access the values related to a particular key using the get() method.

// Creating the new hashmap. Data type of the key 
// is number and data type of the value is string.
let hashmap = new Map<Number, string>();
//  Setting up number-string pairs into the hashmap.
hashmap.set(1, "Hello");
hashmap.set(2, "World!");

// get the values from the hashmap
let value1: string | undefined = hashmap.get(1); 
// If hashmap doesn't contains key-value pair, it returns the u
//ndefined that's why we need to set type of value1 variable to undefined.
console.log("Value for the key 1 is " + value1);
let value2: string | undefined = hashmap.get(2);
console.log("Value for the key 2 is " + value2);

On compiling, it will generate the following JavaScript code −

// Creating the new hashmap. Data type of the key 
// is number and data type of the value is string.
var hashmap = new Map();
//  Setting up number-string pairs into the hashmap.
hashmap.set(1, "Hello");
hashmap.set(2, "World!");
// get the values from the hashmap
var value1 = hashmap.get(1);
// If hashmap doesn't contains key-value pair, it returns the u
//ndefined that's why we need to set type of value1 variable to undefined.
console.log("Value for the key 1 is " + value1);
var value2 = hashmap.get(2);
console.log("Value for the key 2 is " + value2);

Output

The above code will produce the following output −

Value for the key 1 is Hello
Value for the key 2 is World!

Example

In the example below, we create a map and store it in the “hashmap” and get the size of the hashmap. To get the size, we use the ‘size’ variable of the map class.

let hashmap = new Map<Number, string>();
//  Setting up number-string pairs into the hashmap.
hashmap.set(1, "Hello");
hashmap.set(2, "World!");

// get the size of hashmap
console.log(" ");
console.log("The size of the hashmap is " + hashmap.size);

On compiling, it will generate the following JavaScript code −

var hashmap = new Map();
//  Setting up number-string pairs into the hashmap.
hashmap.set(1, "Hello");
hashmap.set(2, "World!");
// get the size of hashmap
console.log(" ");
console.log("The size of the hashmap is " + hashmap.size);

Output

The above code will produce the following output −

The size of the hashmap is 2

Example

After that, we use the has() method to check whether a particular key exists in the hashmap.

let hashmap = new Map<Number, string>();

hashmap.set(1, "Welcome back");
hashmap.set(2, "to Tutorials Point");

console.log("hashmap contains the key 1? " + hashmap.has(1)); 
// returns true as it contains key 1.
console.log("hashmap contains the key 3? " + hashmap.has(3)); 
// returns false as the key 3 does not exist.

On compiling, it will generate the following JavaScript code −

var hashmap = new Map();
hashmap.set(1, "Welcome back");
hashmap.set(2, "to Tutorials Point");
console.log("hashmap contains the key 1? " + hashmap.has(1));
// returns true as it contains key 1.
console.log("hashmap contains the key 3? " + hashmap.has(3));
// returns false as the key 3 does not exist.

Output

The above code will produce the following output −

hashmap contains the key 1? true
hashmap contains the key 3? false

Example

In this example, we delete the two key-value pairs from the hashmap. We also delete all map records using the clear() method.

// Creating the new hashmap. 
let hashmap = new Map<Number, string>();
//  Setting up number-string pairs into the hashmap.
hashmap.set(1, "Welcome");
hashmap.set(2, "To Tutorials Point");
// delete the key 2
hashmap.delete(2);
console.log("Hashmap contains the key 2 ? " + hashmap.has(2)); 
// returns false as we deleted the key 2.

// Clear the hashmap
console.log(" ");
hashmap.clear();
console.log("The size of the hashmap after clearing is :");
console.log(hashmap.size); 
// It returns 0 as we cleared the whole hashmap.

On compiling, it will generate the following JavaScript code −

// Creating the new hashmap. 
var hashmap = new Map();
//  Setting up number-string pairs into the hashmap.
hashmap.set(1, "Welcome");
hashmap.set(2, "To Tutorials Point");
// delete the key 2
hashmap["delete"](2);
console.log("Hashmap contains the key 2 ? " + hashmap.has(2));
// returns false as we deleted the key 2.
// Clear the hashmap
console.log(" ");
hashmap.clear();
console.log("The size of the hashmap after clearing is :");
console.log(hashmap.size);
// It returns 0 as we cleared the whole hashmap.

Output

The above code will produce the following output −

Hashmap contains the key 2 ? false
The size of the hashmap after clearing is :
0

In this tutorial, users learned the basic use of hashmap. From above examples, users learned to use methods of hashmap in TypeScript.

Updated on: 07-Oct-2023

28K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements