ListMap in Scala - GeeksforGeeks

04 Aug, 2021
Immutable maps Implemented by using a list-based data structure. The Scala List class holds a sequenced, linear list of items. We must import scala.collection.mutable.ListMap for ListMap. ListMap collection used only for a small number of elements.Syntax:  
var listMapName = ListMap("k1"->"v1", "k2"->"v2", "k3"->"v3", ...) 
Here, k is key and v is value. 
Creating an ListMap: In below code we can see a ListMap is created with values.
Scala
// Scala program to create or print ListMapimport scala.collection.immutable.ListMap // Creating objectobject Geeks{     // Main method    def main(args: Array[String])    {        // Creating ListMap with values        var listMap = ListMap("C"->"Csharp", "S"->"Scala", "J"->"Java")                 // Printing ListMap        println(listMap)    }}
Output:  
Map(C -> Csharp, S -> Scala, J -> Java)
 Adding and accessing elements : A ListMap is created, add elements and access elements also performed.
Scala
// Scala program to Adding and Accessing Elements ListMapimport scala.collection.mutable.ListMap // Creating objectobject Geeks{     // Main method    def main(args: Array[String])    {        // Creating ListMap        var listMap = ListMap("C"->"Csharp", "S"->"Scala", "J"->"Java")                 // Iterating elements        listMap.foreach        {            case (key, value) => println (key + " -> " + value)                }                 // Accessing value by using key        println(listMap("S"))                 // Adding element        var ListMap2 = listMap + ("P"->"Perl")        ListMap2.foreach        {            case (key, value) => println (key + " -> " + value)        }    }}
Output:  
J -> Java
C -> Csharp
S -> Scala
Scala
P -> Perl
C -> Csharp
J -> Java
S -> Scala
  Removing an element from ListMap : A ListMap is created than removing an element is performed using – sign. Below is the example to removing an element from ListMap. 
Scala
// Scala program to removing Element from ListMapimport scala.collection.mutable.ListMap // Creating objectobject Geeks{     // Main method    def main(args: Array[String])    {        // Creating ListMap        var listMap = ListMap("C"->"Csharp", "S"->"Scala", "J"->"Java")                 // Iterating elements        listMap.foreach        {            case (key, value) => println (key + " -> " + value)            }                 // Removing an element        listMap -= "C"                 println("After Removing")        listMap.foreach        {            case (key, value) => println (key + " -> " + value)        }    }}
Output:  
J -> Java
C -> Csharp
S -> Scala
After Removing
J -> Java
S -> Scala
  Creating an empty ListMap: An empty ListMap is created either by calling its constructor or using ListMap.empty method.
Scala
// Scala program to Create an empty ListMapimport scala.collection.mutable.ListMap // Creating objectobject Geeks{     // Main method    def main(args: Array[String])    {        // Creating an empty list map by calling constructor        var ListMap1 = new ListMap()                   // Creating an empty list map by using .empty method        var ListMap2 = ListMap.empty                 // Printing empty ListMap        println(emptyListMap1)         println(emptyListMap2)     }}
Output:  
Map()
Map()
 
surinderdawra388
scala-collection
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Type Casting in Scala
Class and Object in Scala
Scala Lists
Scala Tutorial – Learn Scala with Step By Step Guide
Operators in Scala
Inheritance in Scala
Scala Constructors
Scala | Arrays
Scala String substring() method with example
Lambda Expression in Scala