DEV Community

Ozoemena
Ozoemena

Posted on

Map data structure

Introduction

Map data structure is also known as a dictionary, associative array or hash map.
It’s a data structure that stores a collection of key-value pairs where each key is associated with a single value. Map remembers the original insertion order of the key, this helps to provide an efficient way of storing and retrieving data based on a unique identifier (the key). It is efficient as it allows for fast lookup, insert and delete.
• In this article, you will be learning
• map methods
• Use of the different map methods
• Types of map

  1. Hash Map
  2. Tree Map
  3. Linked Hash Map
  4. Tries Map
  5. Bloom Filter Map

Map Method

Different methods can be used to manipulate a Map such as

- Map() constructor

This is used to create a map object. We can create a map using the new keyword. Without the new keyword, the browser will throw a TypeError
Syntax: new Map(iterable)
In this case, the iterable can be a two-element array or any other iterable object with a key-value pair element.
For example

Const cars  = new Map([
      [“Toyota”, 20],
      [“Innoson”, 54],
      [“Honda”, 64],
      [“Accura”, 14]
  ])
Enter fullscreen mode Exit fullscreen mode

- set()

Having created a Map, we can decide to add an element to a map or even change an existing map value.

Const cities  = new Map()
 cities.set(“Enugu”, 1996)
 cities.set(“London”, 1902)
 cities.set(“Bangui”, 1956)
Enter fullscreen mode Exit fullscreen mode

In the above examples, we are adding name of cities and their value to the cities map. To change the value of one of the cities, this is how to do it
cities.set(“Enugu”, 1994)

- get()

We can actually get the value of a key in a map using the get method. This takes in the key as a parameter. For example

Const cars = new Map()
Cars.set(“car”, “Innoson”)
Cars.get(“car”) // Innoson
Enter fullscreen mode Exit fullscreen mode

- forEach()

This is a map method that executes a callback function for every member of the map object. This function have value and key as argument. It is actually an iteration method.
cars.forEach((value, key) => {…})
For example

 Cities.forEach((value, key) => {
      return console.log(`This is my city, ${key} established in the year ${value}`)
    })
Enter fullscreen mode Exit fullscreen mode

- The delete(“key”)

This deletes a particular element using the specified key in the map

- The clear()

method, clears the whole element in the map object

Types of Map

1. Hash Map

This uses a hash function to map a key to an indices in an array. It takes a key as an input and produces an index to which a value can be stored. Its time complexity is 1(O) for operations like insertion and retrievals

2. Tree Map.

This is the type of map that is employed in binary search tree. In this type of map, the key is stored in a sorted order. This allows for an efficient searching, insertion and deletion operation. The time complexity is O(Logn)

3. Linked Hashed Map.

Linked hash map is just link the hash map only that it keeps track and order of insertion. It provides where the elements can be assessed in their insertion order.
It is just an extension of the hash map and implements the map interface. It uses hash to replace the key, this makes search and insertion faster. Since it is an extension of hash map, data are stored in a key-value pair. That is, for every value, there is a key associated with it. Since linked hash map stores the insertion order, the node parameter contains address of the next node in the linked hash map. While previous contains address of the previous node.

Feature of linked hash map

• It contains value based on the key
• They contain unique element
• They can have multiple null value but one null key
• They are non-synchronized
• It is the same as hash map but only differ in that it keeps the order of insertion.

4. Tries Map:

This is used to find string with a given prefix. It stores a set of string where each node in the tree represent a prefix of one or more string.

5. Bloom Filter Map

This uses a bloom filter to determine if a key is present or not
Map data structure uses object as key and associate a value (of any type) to that object.
In map data structure, iteration is done over the entries and not the value. An entry is actually a key-value pair, that is a tuple.

Conclusion

In this article we have being able to learn what map data structure is, map methods, their uses, types of map such as hash map, linked hash map, tree map, tries map, bloom filter map and their uses.

Top comments (0)