HashMap in Scala

09 Aug, 2019
HashMap is a part of Scala Collection’s. It is used to store element and return a map. A HashMap is a combination of key and value pairs which are stored using a Hash Table data structure. It provides the basic implementation of Map.
Syntax:
var hashMapName = HashMap("key1"->"value1", "key2"->"value2", "key3"->"value3", ...) 
We must import scala.collection.mutable.HashMap for HashMap.
Creating an HashMap:
Below is the example to create HashMap. In below code, we can see an empty HashMap is created then a HashMap is created with values.
// Scala program to create or print HashMapimport scala.collection.mutable.HashMap  // Creating object object Geeks {       // Main method     def main(args: Array[String])     {         // Creating empty HashMap        var hashMap = new HashMap()                    // Creating HashMap with values        var hashMap2 = HashMap("C"->"Csharp", "S"->"Scala", "J"->"Java")                    // Printing HashMap        println(hashMap)          println(hashMap2)      } } 
Output:
Map()
Map(S -> Scala, J -> Java, C -> Csharp)
 Adding and Accessing Elements :
In the below example, A HashMap is created. add elements and access elements also performed.
// Scala program to Adding and Accessing Elements HashMapimport scala.collection.mutable.HashMap  // Creating object object Geeks {       // Main method     def main(args: Array[String])     {         // Creating HashMap        var hashMap = HashMap("C"->"Csharp", "S"->"Scala", "J"->"Java")                  // Iterating elements        hashMap.foreach         {              case (key, value) => println (key + " -> " + value)                 }                    // Accessing value by using key         println(hashMap("S"))                    // Adding element        var HashMap2 = hashMap + ("P"->"Perl")          HashMap2.foreach         {              case (key, value) => println (key + " -> " + value)          }      } } 
Output:
S -> Scala
J -> Java
C -> Csharp
Scala
S -> Scala
P -> Perl
J -> Java
C -> Csharp
 Removing an element from HashMap :A HashMap is created than removing an element is performed using – sign. Below is the example to removing an element from HashMap.
// Scala program to removing Element HashMapimport scala.collection.mutable.HashMap  // Creating object object Geeks {       // Main method     def main(args: Array[String])     {         // Creating HashMap        var hashMap = HashMap("C"->"Csharp", "S"->"Scala", "J"->"Java")                  // Iterating elements        hashMap.foreach         {             case (key, value) => println (key + " -> " + value)                 }                   // Removing an element        hashMap -= "C"                  println("After Removing")        hashMap.foreach         {             case (key, value) => println (key + " -> " + value)         }     } } 
Output:
S -> Scala
J -> Java
C -> Csharp
After Removing
S -> Scala
J -> Java
scala-collection
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
For Loop in Scala
Scala | map() method
Scala | flatMap Method
Scala | reduce() Function
String concatenation in Scala
Type Casting in Scala
Scala Tutorial – Learn Scala with Step By Step Guide
Scala List filter() method with example
Scala String substring() method with example
How to Install Scala with VSCode?