How to Convert List to Map in Scala

Suraj P Feb 02, 2024
  1. List and Map in Scala
  2. Convert List to Map in Scala
How to Convert List to Map in Scala

This article will teach how to convert a List to a Map in Scala.

List and Map in Scala

First, let’s look at brief definitions of List and Map.

  1. A list is a collection of similar kinds of immutable data. It is used to represent a linked list in Scala.

    Syntax:

    list_name = List( element1, element2 , element3....)
    

    Example:

    val fruits = List("Orange", "Apple" , "Mango" )
    
  2. Map in Scala is a collection of key-value pairs. Keys are always unique, whereas values can be repeated; they are highly useful for data retrieval.

    Maps in Scala can be immutable or mutable, but by default, they are immutable.

    Syntax:

    map_name = Map(key1->value1, key2->value2, key3->value3 ....)
    

    Example:

    val marks = Map("tony" -> 30, "bruce" -> 20,"Iron Man" -> 50)
    

Convert List to Map in Scala

To convert a list into a map in Scala, we use the toMap method. We must remember that a map contains a pair of values, i.e., key-value pair, whereas a list contains only single values.

So we have two ways to do so:

  1. Using the zipWithIndex method to add indices as the keys to the list.
  2. Merging two lists considering one as the keys and the other as values.

the zipWithIndex Method in Scala

We will use the zipWithIndex method and Scala’s toMap method to add keys to the list.

Syntax:

map_name = list_name.zipWithIndex.map{ case(k,v) => (v,)}.toMap

Example code:

object myClass {

    def main(args: Array[String]) {

        val fruits = List("Apple", "Orange" , "Watermelon" , "Mango")
        val map = fruits.zipWithIndex.map{ case (k,v) => (v,k) }.toMap

        println("The values of map : "+ map)
    }
}

Output:

The values of map : Map(0 -> Apple, 1 -> Orange, 2 -> Watermelon, 3 -> Mango)

In the above code, we have created a list of fruits containing the names of different fruits. We have the toMap method to convert the list to the map.

The list contains only single values; we have used the zipWithIndex method to add index values as keys, starting from 0 to every list element.

Merge Two Lists to Convert List to Map in Scala

We can merge two lists to create a map; here, we use one list as keys and the other as values at the end, creating key-value pairs for the map.

A few points to keep in mind while merging two lists are:

  1. The list we are using as a key should have unique elements. If the elements are unique, then the later element will be considered.
  2. Both lists should have an equal number of elements; a value should be present for every key. If that’s not the case, then the excess elements of the list will be ignored.

Syntax:

val map_name = (list_1 zip list_2).toMap

Example code:

object myClass {

    def main(args: Array[String]) {

        val names = List("Tony", "Bruce" , "Strange" , "Iron Man")
        val marks = List(33 ,56 ,89 ,99)
        val result = (names zip marks).toMap

        println("The values of map : "+ result)
    }
}

Output:

The values of map : Map(Tony -> 33, Bruce -> 56, Strange -> 89, Iron Man -> 99)

We have two lists, names and marks in the above code. We have used the toMap method to convert a list to a map, list names is used as a key, and list marks is used as a value, and we have merged them using zip and stored the resulting map in the map result.

Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - Scala List