EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Hashmap
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript Hashmap

TypeScript Hashmap

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Below is the Syntax for Typescript Map Interface:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,883 ratings)

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:

TypeScript Hashmap 1

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:

Iterating over Map keys value and entries

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:

TypeScript Hashmap 3

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

This is a guide to TypeScript Hashmap. Here we discuss the introduction to TypeScript Hashmap along with examples, respectively. You may also have a look at the following articles to learn more –

  1. TypeScript Functions
  2. TypeScript Array
  3. TypeScript Versions
  4. TypeScript 2.x
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more