Map in scala

Map in SCala
Reading Time: 3 minutes

Hi everyone, today I’ll be discussing different types of Map is Scala. I will try to differentiate between the different types of maps to make it easy to understand. So, let’s begin !!

What is a Map in Scala?

A map is a collection in Scala. It is a collection of key/value pairs. The key and value pairs are also known as mappings and associations.

In the following way, by using Tuple2 we can create a Map.

A map in Scala
A map in Scala

The following is another way that we can use to create a Map. The ‘ ->’ is an implicit method that Scala’s Predef Object offers.

Creating map using ->
Creating map using ->

Key: The key in the map is unique and we can use the key to retrieve the value. In ‘otherMap’, 1 and 2 are key.

Value: A value is linked to a key. It need not to be unique.

We can have mutable as well as an immutable map. The difference between the two is that we can change a mutable map whereas we can’t change an immutable map. When we do and updation/addition/deletion in the mutable map, the change is in the same object. But doing the same in an immutable map creates a new object with every change.

Let’s look at an example to understand it.

Updating in an immutable map
Updating in an immutable map

In an immutable map, we can see that even after updating ‘othermap’, I got NoSuchElementException when I try to retrieve the value for key 3. Also, after updating ‘othermap’, ‘res1’ is created and I can retrieve the value for 3 in res1. This shows a new object is created after updating.

A mutable map
A mutable map

In a mutable map, it is evident that the same object ‘mutableMap’ has the changes whereas that is not the case in an immutable map.

Different Maps in Scala

HashMap

A HashMap is a concrete implementation of Map interface. The name given to it is because it uses hashing technique. By using hashing, searching and indexing become fast.

  • It allows a single null key and multiple null values.
  • No guarantee for the order of the elements in the Map.
  • It provides O(1) insertion and lookup.

ListMap

It is a Map implementation done using the list-based data structure. It holds a linear list of elements.

  • Preferably used for a small number of elements.
  • Elements are stored in the reverse order of the insertion of elements.
  • The head and tail operation has O(n) complexity whereas init and last has O(1) complexity.
  • Searching and removing entries is also O(n).

TreeMap

It is a sorted map that uses a red-black tree for its implementation. If you desire a traversal in order of ordering then this Map is useful.

  • It can’t have a null key but multiple null values.
  • Also, it offers O(log N) lookup and insertion.

LinkedHashmap

A LinkedHashmap is similar to HashMap but it also maintains insertion order. That is an advantage over HashMap.

  • It allows a single null key and multiple null values.
  • Offers O(1) insertion and lookup.
  • It takes advantage of Hashing and also maintains insertion order.

To conclude, if you want to have fast lookup and insertion but order does not matter then HashMap is best to use. If you want keys in sorted order then a SortedMap/TreeMap is useful. But if you want to maintain the elements in the insertion order and also take advantage of Hashing technique then you should go for LinkedHashMap. You can also use a ListMap if you want to maintain insertion order but you have to keep in mind that searching and insertion is costly. The more the number of elements in ListMap the costlier it is.

That’s all from my side. I hope this was helpful and is good to understand the difference between the different maps. Bye for now…!!!

References


Knoldus-blog-footer-image

Written by 

Muskan Gupta is a Software Consulatant at Knoldus Inc having an experience of 2 years. She is passionate about Scala development and she has sound knowledge of Scala, Akka, Akka-Streams, Alpakka and Akka Http. She has interest in exploring different technologies.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading