How to convert List to Map in Scala?

Scala | Converting List to Map: Here, we are going to learn how to convert list to map in Scala with syntaxes and working examples?
Submitted by Shivang Yadav, on May 21, 2020

List in Scala

List is a collection of immutable data of the same type. List represents a LinkedList in Scala.

Creating a list:

Syntax to create a list in Scala,

    val list_name = List (element1, elemenet2, ...)

Example of list:

    // List of integers
    List(3, 65, 1, 90, 54, 100)
    // List of strings
    List("scala", "C++", "JavaScript", "Python") 

Maps in Scala

Map is a collection of elements stored in the form of key-value pairs. They have unique keys that are used to retrieve the value related to the key.

Creating a Map in Scala:

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

Example of Map:

    Map(int -> string): Map(1 -> "Scala" , 2 -> "JavaScript", 3-> "Python")

Convert List to Map in Scala

In Scala, you can convert a list to a map in Scala using the toMap method.

A map contains a set of values i.e. key-> value but a list contains single values. So, while converting a list to map we have two ways,

  1. Add index to list.
  2. Merge two lists, use one as key, and other as value.

Converting list to map by adding index to the list

We will add index as key values of the elements of the list. For this we will use the zipWithIndex method.

Syntax:

    List_name .zipWithIndex.map{ case(v,i) => (i,)}.toMap

Program to convert list to map

object MyObject {
    
    def main(args: Array[String]) {
        val progLang = List("C++", "JavaScript" , "Scala" , "Python")
        val map = progLang.zipWithIndex.map{ case (v,i) => (i,v) }.toMap
        println("The values of map : "+ map)
    }
}

Output:

The values of map : Map(0 -> C++, 1 -> JavaScript, 2 -> Scala, 3 -> Python)

Explanation:

In the above code, we have created a list progLang which contains names of programming languages. To convert this list to a map, we have used the toMap method. As the list has single value and map needs to values we have used indexes as key using the zipWithIndex method that adds index values starting from 0 to every element of the list in the map. Using these function we have created the Map with name map and printed its contents using the println method.

Converting List to map by merging two list

We can create a map by using two lists and merging them to create a map. For creation, we will add using one of the lists as key and other as value for the key-value pairs of the map.

The list to be used as the key in the map should have unique elements and both the lists should have the same number of elements.

Syntax:

    map_name = (list1_name zip list2_name).toMap

Program to convert lists to map

object MyObject {
    
    def main(args: Array[String]) {
        val myBikes = List("ThunderBird 350" , "YRF R3" , "S1000RR")
        val topSpeed = List(132 , 167 , 300)
        val bikeSpeed = (myBikes zip topSpeed).toMap
        println("The values of map : "+ bikeSpeed)
    }
}

Output:

The values of map : Map(ThunderBird 350 -> 132, YRF R3 -> 167, S1000RR -> 300)

Explanation:

In the above, code we have created two lists, myBike to store names of bikes and topSpeed to store the top speed of these bikes. Now, we have used the toMap method to convert the list to the map. To use one list as key and other as value, we have used the zip. Then we have stored the map to bikeSpeed variable and print its contents using println.

Points to remember:

  • While converting the elements of both lists should be the same otherwise the elements that are excess in the list will be left (will not be considered).
  • If the elements of the list to be used as keys are not unique the later element will be considered in the map and the match value for the first one will be discarded.



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.