Updated June 12, 2023
Introduction to TypeScript Hashmap
TypeScript Hashmap defines the type of key and value that it holds. Hashmap in TypeScript has a Generic type as ‘Map’ and is modified frequently for better performance. Hashmap even protects with type safety. Regarding the TypeScript performance, object literals are slow if a user modifies the TypeScript Hashmap. Particularly, adding and removing or deleting of key-value pairs are the slowest operations performed on hashmap. It is obvious for less performance while deletion of key-value pairs in TypeScript hashmap. Object literals are objects, hence while deleting any key-value properties will be hectic for a user. A dedicated data type has been introduced in TypeScript as ‘Map’ to address all these issues. A map is an interface which defines how key-value pairs can be used; the implementation of this interface, ‘Map’, is TypeScript Hashmap.
Syntax:
Below is the Syntax for Typescript Map Interface:
var map = new Map();
This line above is used to create a Map Interface; implementing this would give us Hashmap.
We need not pass any kind of arguments here in creation, while we need to pass arguments or also known as key-value pairs, to TypeScript Map methods listed below:
- get(key): This method is used to retrieve data from the Map; it returns undefined if there is no key existing in the map.
- set(key, value): This method is used to add key-value pairs to the Map.
- has(key): This method will check whether the specified key is present in the Map or not. If the key is present, it will return True else False.
- delete(key): This method is used to remove the key-value pairs based on the key.
- clear(): This method is used to clear out the entries of the map.
- size(): This method is used to return the size of the Map.
TypeScript hashmap implementation is a data structure added with EcmaScript 6 of JavaScript. TypeScript allows to store these key-value pairs to remember the order of insertion of keys. Here, the user can use any of the value as a key or as a value.
Examples of TypeScript Hashmap
Given below are the examples of TypeScript Hashmap:
Example #1
Creation of Typescript map.
Code:
let map = new Map();
map.set('22', 'Infosys');
map.set(32, 'TCS');
map.set(false,'Google');
map.set('45','Facebook');
map.set('65','Amazon');
console.log("Employer at index 22: " +map.get('22'));
console.log("Employer at index 32: " +map.get(32));
console.log("Size of the map: " +map.size);
console.log("Deleting a value: " +map.delete('65'));
console.log( "New Size of the map: " +map.size);
Output:
So here we are, Creating a TypeScript map Interface and assigning or inserting some of the key-value pairs. We are displaying some of the rows using the get method using an index. Size of the map interface and also like deleting the key-value pair using the key index. So the new size of the map interface is changed. Hashmap is a dynamic type of map, whereas Map is the static type of map. Which actually means that the compiler will treat the map object as one of the type Map, even at runtime. Typescript Hashmap can be instantiated and assigned to a Map variable as Implementation of Map Interface is Hashmap. Hashmap can contain multiple key-value pairs but cannot contain duplicate keys.
Example #2
Iterating over Map keys value and entries.
Code:
let employeeMap = new Map();
employeeMap.set("Saideep", 20);
employeeMap.set("Karthik", 25);
employeeMap.set("Sumit", 21);
employeeMap.set("Sameer", 22);
employeeMap.set("Raje", 24);
//Iterating over map keys
for (let name of employeeMap.keys()) {
console.log("Employee Names= " +name);
}
//Iterating over map values
for (let age of employeeMap.values()) {
console.log("Employee Age= " +age);
}
console.log("The employeeMap Entries are: ");
//Iterating over map entries
for (let entry of employeeMap.entries()) {
console.log(entry[0], entry[1]);
}
Output:
Here, a Map interface is declared or created and is looping based on the keys, values, and entries.
Example #3
Hashmap implementation of Map interface with all the methods.
Code:
let nameMap = new Map();
nameMap.set("Anand",1001);
nameMap.set("Bhargavi",1002);
nameMap.set("Chrestin",1003);
nameMap.set("Daniel",1004);
nameMap.set("Esther",1005);
console.log(nameMap.size);
console.log(nameMap.get("Chrestin"));
console.log(nameMap.get("Esther"));
//Iterating map keys
for (let item of nameMap.keys()) {
console.log("Names: "+item);
}
//Iterating map values
for (let item of nameMap.values()) {
console.log("ID: ",item);
}
//Iterating map entries
for (let item of nameMap.entries()) {
console.log("entries: ", item[0], item[1]);
}
//Destructuring on object entries
for (let [key, value] of nameMap) {
console.log("key value pairs: ", key, value);
}
// item Daniel will get deleted, will return 'true' as output
nameMap.delete("Daniel")
// Clear all the entries of the map
nameMap.clear();
console.log(nameMap.size);
Output:
So here we have worked on all the method of TypeScript hashmap.
Conclusion
With this, we conclude our topic ‘TypeScript Hashmap’, which is the implementation of Map Interface. We have seen the syntax of Map and the map methods, get, set, delete, has, etc. Worked on the 3 examples above, which will surely help you implement the basic Map in any type of Scripting language. The point to note is Hashmapping does not entertain Duplicate keys.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Hashmap” was beneficial to you. You can view EDUCBA’s recommended articles for more information.