

{"id":13992,"date":"2018-04-19T09:21:20","date_gmt":"2018-04-19T09:21:20","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=13992"},"modified":"2021-12-04T10:16:24","modified_gmt":"2021-12-04T04:46:24","slug":"scala-map","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/scala-map\/","title":{"rendered":"Learn Scala Map with Examples Quickly &amp; Effectively"},"content":{"rendered":"<p>In this tutorial on Scala Map, we will see how to define and process maps, and what methods to call on them. We will learn to declare a Scala Map, Operations on a Map in Scala, Concatenating Maps, Printing Keys and Values from a Scala Map, Searching for a Key in a Map, Methods to Call on a Map etc.<\/p>\n<p>So,\u00a0let&#8217;s begin Scala Map Tutorial.<\/p>\n<h3>An Introduction to Maps in Scala<\/h3>\n<p>A Map in Scala is a collection of key-value pairs, and is also called a hash table. We can use a key to access a value. These keys are unique; however, the values may be common. The default Scala Map is immutable. To use a mutable Map, we use the scala.collection.mutable.Map class.<\/p>\n<p>To use both in the same place, refer to the immutable Map as Map, and to the mutable Map as mutable.Map. I also recommend you refer our latest blog on <strong><a href=\"https:\/\/data-flair.training\/blogs\/scala-closures\/\">Scala Closures<\/a><\/strong> which is explained in detail with Examples. For now let&#8217;s jump to learn to declare a Scala Map.<\/p>\n<h3>Declaring a Scala Map<\/h3>\n<p>We can either declare an empty Scala map or one with values.<\/p>\n<h4>a. Declaring an Empty Scala Map<\/h4>\n<pre class=\"EnlighterJSRAW\">scala&gt; var m:Map[String,Int]=Map()\r\nm: Map[String,Int] = Map()<\/pre>\n<p>Here, we must include the type annotation so it can assign proper types to the variables.<\/p>\n<h4>b. Declaring a Scala Map with Values<\/h4>\n<p>When we provide values, Scala will use those to infer the type of variables. So, we don\u2019t need to include the type annotations.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var m=Map(\"Ayushi\"-&gt;0,\"Megha\"-&gt;1)\r\nm: scala.collection.immutable.Map[String,Int] = Map(Ayushi -&gt; 0, Megha -&gt; 1)<\/pre>\n<p>Now, to add a key-value pair to this, we do the following:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m+=(\"Ruchi\"-&gt;2)\r\nscala&gt; m\r\nres1: scala.collection.immutable.Map[String,Int] = Map(Ayushi -&gt; 0, Megha -&gt; 1, Ruchi -&gt; 2)<\/pre>\n<p><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/scala-array\/\">Scala Arrays and Multidimensional Arrays in Scala<\/a><\/strong><\/p>\n<h3>Operations on a Map in Scala<\/h3>\n<p>These are the basic operations we can carry out on a Map:<\/p>\n<h4>a. keys<\/h4>\n<p>This returns an iterable with each key in the Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.keys\r\nres2: Iterable[String] = Set(Ayushi, Megha, Ruchi)\r\nscala&gt; m(\"Megha\")\r\nres56: Int = 1<\/pre>\n<h4>b. values<\/h4>\n<p>This returns an iterable with each value in the Scala Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.values\r\nres3: Iterable[Int] = MapLike.DefaultValuesIterable(0, 1, 2)<\/pre>\n<h4>c. isEmpty<\/h4>\n<p>If the Map is empty, this returns true; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.isEmpty\r\nres4: Boolean = false\r\nscala&gt; Map().isEmpty\r\nres5: Boolean = true<\/pre>\n<h3>Concatenating Maps in Scala<\/h3>\n<p>We can concatenate\/joing two Maps in more than one way. Let\u2019s take another Scala Map for this.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var m1=Map(\"Megha\"-&gt;3,\"Ruchi\"-&gt;2,\"Becky\"-&gt;4)\r\nm1: scala.collection.immutable.Map[String,Int] = Map(Megha -&gt; 3, Ruchi -&gt; 2, Becky -&gt; 4)<\/pre>\n<h3>The ++ Operator<\/h3>\n<pre class=\"EnlighterJSRAW\">scala&gt; m++m1\r\nres6: scala.collection.immutable.Map[String,Int] = Map(Ayushi -&gt; 0, Megha -&gt; 3, Ruchi -&gt; 2, Becky -&gt; 4)\r\nscala&gt; m1++m\r\nres7: scala.collection.immutable.Map[String,Int] = Map(Megha -&gt; 1, Ruchi -&gt; 2, Becky -&gt; 4, Ayushi -&gt; 0)<\/pre>\n<p>See the difference in the values for \u201cMegha\u201d in both cases?<br \/>\n<strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/scala-operator\/\">Scala Operator<\/a><\/strong><\/p>\n<h3>Printing Keys and Values from a Map<\/h3>\n<p>We can use a foreach loop to walk through the keys and values of a Scala Map:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.keys.foreach{i=&gt;println(i+\" \"+m(i))}<\/pre>\n<p>Ayushi 0<br \/>\nMegha 1<br \/>\nRuchi 2<\/p>\n<h3>Searching for a Key in a Scala Map<\/h3>\n<p>The Map.contains() method will tell us if a certain key exists in the Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.contains(\"Ayushi\")\r\nres10: Boolean = true\r\nscala&gt; m.contains(\"Fluffy\")\r\nres11: Boolean = false<\/pre>\n<p>Any Doubt yet in Scala Map? Please Comment.<\/p>\n<h3>Methods to Call on a Scala Map<\/h3>\n<p>We can call the following methods on a Map. (Note that they don\u2019t modify the original Scala Map)<\/p>\n<h4>a. def ++(xs: Map[(A, B)]): Map[A, B]<\/h4>\n<p>This concatenates two Maps.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.++(m1)\r\nres15: scala.collection.immutable.Map[String,Int] = Map(Ayushi -&gt; 0, Megha -&gt; 3, Ruchi -&gt; 2, Becky -&gt; 4)\r\nscala&gt; m1.++(m)\r\nres16: scala.collection.immutable.Map[String,Int] = Map(Megha -&gt; 1, Ruchi -&gt; 2, Becky -&gt; 4, Ayushi -&gt; 0)<\/pre>\n<h4>b. def -(elem1: A, elem2: A, elems: A*): Map[A, B]<\/h4>\n<p>This returns a new Map eliding the pairs for the keys mentioned in the arguments.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.-(\"Ayushi\",\"Ruchi\")\r\nres21: scala.collection.immutable.Map[String,Int] = Map(Megha -&gt; 1)<\/pre>\n<h4>c. def get(key: A): Option[B]<\/h4>\n<p>This returns the value associated with the key; it returns this as an Option.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.get(\"Megha\")\r\nres27: Option[Int] = Some(1)\r\nscala&gt; m.get(\"Fluffy\")\r\nres28: Option[Int] = None<\/pre>\n<h4>d. def iterator: Iterator[(A, B)]<\/h4>\n<p>This returns an iterator over the Scala Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.iterator\r\nres29: Iterator[(String, Int)] = non-empty iterator<\/pre>\n<p class=\"entry-title \"><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/scala-array\/\">Scala Arrays and Multidimensional Arrays in Scala<\/a><\/strong><\/p>\n<h4>e. def addString(b: StringBuilder): StringBuilder<\/h4>\n<p>This appends all elements of the Map to the String Builder.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.addString(new StringBuilder())\r\nres30: StringBuilder = Ayushi -&gt; 0Megha -&gt; 1Ruchi -&gt; 2<\/pre>\n<h4>f. def addString(b: StringBuilder, sep: String): StringBuilder<\/h4>\n<p>This does what the above method does, except it introduces a separator between the pairs.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.addString(new StringBuilder(),\"*\")\r\nres31: StringBuilder = Ayushi -&gt; 0*Megha -&gt; 1*Ruchi -&gt; 2<\/pre>\n<h4>g. def apply(key: A): B<\/h4>\n<p>This searches for a key in the Scala Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.apply(\"Fluffy\")\r\njava.util.NoSuchElementException: key not found: Fluffy\r\n at scala.collection.immutable.Map$Map3.apply(Map.scala:167)\r\n ... 28 elided\r\nscala&gt; m.apply(\"Ayushi\")\r\nres33: Int = 0<\/pre>\n<h4>h. def clear(): Unit<\/h4>\n<p>This actually removes all bindings from the Map in Scala.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; import scala.collection.mutable.Map\r\nimport scala.collection.mutable.Map\r\nscala&gt; var m2=scala.collection.mutable.Map(\"One\"-&gt;1,\"Two\"-&gt;2,\"Three\"-&gt;3)\r\nm2: scala.collection.mutable.Map[String,Int] = Map(One -&gt; 1, Two -&gt; 2, Three -&gt; 3)\r\nscala&gt; m2.clear()\r\nscala&gt; m2\r\nres36: scala.collection.mutable.Map[String,Int] = Map()<\/pre>\n<p>Doing this to m1 will result in the following error:<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m1.clear()\r\n&lt;console&gt;:13: error: value clear is not a member of scala.collection.immutable.Map[String,Int]\r\n      m1.clear()\r\n         ^<\/pre>\n<h4>i. def clone(): Map[A, B]<\/h4>\n<p>This creates a clone\/copy of the receiver object.<\/p>\n<p>We can\u2019t clone an immutable Map in Scala. So, let\u2019s revive m2.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var m2=scala.collection.mutable.Map(\"One\"-&gt;1,\"Two\"-&gt;2,\"Three\"-&gt;3)\r\nm2: scala.collection.mutable.Map[String,Int] = Map(One -&gt; 1, Two -&gt; 2, Three -&gt; 3)\r\nscala&gt; m2.clone()\r\nres38: scala.collection.mutable.Map[String,Int] = Map(One -&gt; 1, Two -&gt; 2, Three -&gt; 3)<\/pre>\n<h4>j. def contains(key: A): Boolean<\/h4>\n<p>If the Map contains this key, this returns true; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.contains(\"Megha\")\r\nres40: Boolean = true\r\nscala&gt; m.contains(\"Fluffy\")\r\nres41: Boolean = false<\/pre>\n<h4>k. def copyToArray(xs: Array[(A, B)]): Unit<\/h4>\n<p>This fills key-value pairs from the Map into an Array.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var arr:Array[Any]=Array(0,0,0,0,0,0,0,0)\r\narr: Array[Any] = Array(0, 0, 0, 0, 0, 0, 0, 0)\r\nscala&gt; m.copyToArray(arr)\r\nscala&gt; arr\r\nres47: Array[Any] = Array((Ayushi,0), (Megha,1), (Ruchi,2), 0, 0, 0, 0, 0)<\/pre>\n<h4>l. def count(p: ((A, B)) =&gt; Boolean): Int<\/h4>\n<p>This returns the number of key-value pairs in the Scala Map that satisfy the given predicate.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.count(x=&gt;true)\r\nres49: Int = 3<\/pre>\n<p class=\"entry-title \"><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/tuples-in-scala-introduction\/\">Tuples in Scala \u2013 A Quick and Easy Tutorial<\/a><\/strong><\/p>\n<h4>m. def drop(n: Int): Map[A, B]<\/h4>\n<p>This returns all elements except the first n.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.drop(2)\r\nres59: scala.collection.immutable.Map[String,Int] = Map(Ruchi -&gt; 2)<\/pre>\n<h4>n. def dropRight(n: Int): Map[A, B]<\/h4>\n<p>This returns all elements except the last n.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.dropRight(2)\r\nres60: scala.collection.immutable.Map[String,Int] = Map(Ayushi -&gt; 0)<\/pre>\n<h4>o. def dropWhile(p: ((A, B)) =&gt; Boolean): Map[A, B]<\/h4>\n<p>This drops pairs until the predicate becomes false for a pair.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.dropWhile(x=&gt;false)\r\nres61: scala.collection.immutable.Map[String,Int] = Map(Ayushi -&gt; 0, Megha -&gt; 1, Ruchi -&gt; 2)<\/pre>\n<h4>p. def empty: Map[A, B]<\/h4>\n<p>This returns an empty Map of the same kind.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.empty\r\nres68: scala.collection.immutable.Map[String,Int] = Map()<\/pre>\n<h4>q. def equals(that: Any): Boolean<\/h4>\n<p>This returns true if both Maps contain the same key-value pairs; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.equals(m1)\r\nres69: Boolean = false<\/pre>\n<h4>r. def exists(p: ((A, B)) =&gt; Boolean): Boolean<\/h4>\n<p>If the predicate holds true for some elements of the\u00a0 Scala Map, this returns true; otherwise, false.<\/p>\n<h4>s. def filter(p: ((A, B))=&gt; Boolean): Map[A, B]<\/h4>\n<p>This returns all such elements (see above).<\/p>\n<h4>t. def filterKeys(p: (A) =&gt; Boolean): Map[A, B]<\/h4>\n<p>This returns all pairs where the key satisfies the predicate.<\/p>\n<h4>u. def find(p: ((A, B)) =&gt; Boolean): Option[(A, B)]<\/h4>\n<p>This returns the first element that satisfies the predicate.<\/p>\n<h4>v. def foreach(f: ((A, B)) =&gt; Unit): Unit<\/h4>\n<p>This applies the function to all elements of the Scala Map.<\/p>\n<h4>w. def init: Map[A, B]<\/h4>\n<p>This returns all elements except the last.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.init\r\nres82: scala.collection.immutable.Map[String,Int] = Map(Ayushi -&gt; 0, Megha -&gt; 1)<\/pre>\n<h4>x. def isEmpty: Boolean<\/h4>\n<p>If the Map is empty, this returns true; otherwise, false.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.isEmpty\r\nres83: Boolean = false<\/pre>\n<h4>y. def keys: Iterable[A]<\/h4>\n<p>This returns an iterator over all keys in the Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.keys\r\nres84: Iterable[String] = Set(Ayushi, Megha, Ruchi)<\/pre>\n<h4>z. def last: (A, B)<\/h4>\n<p>This returns the last element from the Map in Scala.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.last\r\nres85: (String, Int) = (Ruchi,2)<\/pre>\n<h4>aa. def max: (A, B)<\/h4>\n<p>This returns the largest element.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.max\r\nres86: (String, Int) = (Ruchi,2)<\/pre>\n<h4>ab. def min: (A, B)<\/h4>\n<p>This returns the smallest element.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.min\r\nres87: (String, Int) = (Ayushi,0)<\/pre>\n<h4>ac. def mkString: String<\/h4>\n<p>This represents the elements of the Map as a String.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.mkString\r\nres88: String = Ayushi -&gt; 0Megha -&gt; 1Ruchi -&gt; 2<\/pre>\n<h4>ad. def product: (A, B)<\/h4>\n<p>This returns the product of all elements of the Scala Map.<\/p>\n<h4>ae. def remove(key: A): Option[B]<\/h4>\n<p>This removes a key from the Map and returns the value. Let\u2019s take a new Map for this.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; var d=Map(1-&gt;2,3-&gt;4,5-&gt;6)\r\nd: scala.collection.mutable.Map[Int,Int] = Map(5 -&gt; 6, 1 -&gt; 2, 3 -&gt; 4)\r\nscala&gt; d.remove(3)\r\nres93: Option[Int] = Some(4)<\/pre>\n<h4>af. def retain(p: (A, B) =&gt; Boolean): Map.this.type<\/h4>\n<p>This retains, in the Map, only the pairs that satisfy the predicate.<\/p>\n<p><strong>Learn: <a href=\"https:\/\/data-flair.training\/blogs\/scala-functions\/\">Scala Functions: Quick and Easy Tutorial<\/a><\/strong><\/p>\n<h4>ag. def size: Int<\/h4>\n<p>This returns the number of elements in the Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.size\r\nres94: Int = 3<\/pre>\n<h4>ah. def sum: (A, B)<\/h4>\n<p>This returns the sum of all elements in the Map in Scala.<\/p>\n<h4>ai. def tail: Map[A, B]<\/h4>\n<p>This returns all elements except the first.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.tail\r\nres97: scala.collection.immutable.Map[String,Int] = Map(Megha -&gt; 1, Ruchi -&gt; 2)<\/pre>\n<h4>aj. def take(n: Int): Map[A, B]<\/h4>\n<p>This returns the first n elements from the Map in Scala.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.take(2)\r\nres98: scala.collection.immutable.Map[String,Int] = Map(Ayushi -&gt; 0, Megha -&gt; 1)<\/pre>\n<h4>ak. def takeRight(n: Int): Map[A, B]<\/h4>\n<p>This returns the last n elements.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.takeRight(2)\r\nres99: scala.collection.immutable.Map[String,Int] = Map(Megha -&gt; 1, Ruchi -&gt; 2)<\/pre>\n<h4>al. def takeWhile(p: ((A, B)) =&gt; Boolean): Map[A, B]<\/h4>\n<p>This returns elements from the Map as long as the predicate is satisfied.<\/p>\n<h4>am. def toArray: Array[(A, B)]<\/h4>\n<p>This returns an Array from the Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.toArray\r\nres100: Array[(String, Int)] = Array((Ayushi,0), (Megha,1), (Ruchi,2))<\/pre>\n<h4>an. def toList: List[A]<\/h4>\n<p>This returns a List from the Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.toList\r\nres101: List[(String, Int)] = List((Ayushi,0), (Megha,1), (Ruchi,2))<\/pre>\n<h4>ao. def toSeq: Seq[A]<\/h4>\n<p>This returns a Sequence from the Scala Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.toSeq\r\nres102: Seq[(String, Int)] = Vector((Ayushi,0), (Megha,1), (Ruchi,2))<\/pre>\n<h4>ap. def toSet: Set[A]<\/h4>\n<p>This returns a Set from the Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.toSet\r\nres103: scala.collection.immutable.Set[(String, Int)] = Set((Ayushi,0), (Megha,1), (Ruchi,2))<\/pre>\n<h4>aq. def toString(): String<\/h4>\n<p>This returns a String from the Map.<\/p>\n<pre class=\"EnlighterJSRAW\">scala&gt; m.toString\r\nres104: String = Map(Ayushi -&gt; 0, Megha -&gt; 1, Ruchi -&gt; 2)<\/pre>\n<p>So, this was all on Scala Map. Hope you like our explanation,<\/p>\n<h3>Conclusion<\/h3>\n<p>A Map in Scala is a collection holding key-value pairs. In this tutorial, we saw how to create, process, and call methods on them.<\/p>\n<p><strong><a href=\"https:\/\/docs.scala-lang.org\/overviews\/collections\/maps.html\">For Reference<\/a><\/strong><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1974,&quot;href&quot;:&quot;https:\\\/\\\/docs.scala-lang.org\\\/overviews\\\/collections\\\/maps.html&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250919083834\\\/https:\\\/\\\/docs.scala-lang.org\\\/overviews\\\/collections\\\/maps.html&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-10 14:36:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-02 16:19:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-07 07:57:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-14 13:01:26&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-20 18:53:56&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-08 11:49:04&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-29 13:09:42&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-06 04:27:33&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-19 00:42:54&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-24 23:28:49&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-29 19:53:15&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-05 00:08:41&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-08 14:34:29&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-12 02:08:57&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-07-12 02:08:57&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial on Scala Map, we will see how to define and process maps, and what methods to call on them. We will learn to declare a Scala Map, Operations on a Map&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":34733,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[2861,3634,3638,7074,9280,12492],"class_list":["post-13992","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scala","tag-concatenating-maps-in-scala","tag-declaring-a-map-with-values","tag-declaring-a-scala-map","tag-introduction-to-maps-in-scala","tag-operations-on-a-map-in-scala","tag-scala-map"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Learn Scala Map with Examples Quickly &amp; Effectively - DataFlair<\/title>\n<meta name=\"description\" content=\"Scala Map: Learn to declare a Scala Map, Operations on a Map in Scala, Concatenating Maps, Printing Keys and Values from a Scala Map and much more...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/scala-map\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn Scala Map with Examples Quickly &amp; Effectively - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Scala Map: Learn to declare a Scala Map, Operations on a Map in Scala, Concatenating Maps, Printing Keys and Values from a Scala Map and much more...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/scala-map\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-19T09:21:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-04T04:46:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Maps-01-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Learn Scala Map with Examples Quickly &amp; Effectively - DataFlair","description":"Scala Map: Learn to declare a Scala Map, Operations on a Map in Scala, Concatenating Maps, Printing Keys and Values from a Scala Map and much more...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/scala-map\/","og_locale":"en_US","og_type":"article","og_title":"Learn Scala Map with Examples Quickly &amp; Effectively - DataFlair","og_description":"Scala Map: Learn to declare a Scala Map, Operations on a Map in Scala, Concatenating Maps, Printing Keys and Values from a Scala Map and much more...","og_url":"https:\/\/data-flair.training\/blogs\/scala-map\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-04-19T09:21:20+00:00","article_modified_time":"2021-12-04T04:46:24+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Maps-01-1.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/scala-map\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/scala-map\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Learn Scala Map with Examples Quickly &amp; Effectively","datePublished":"2018-04-19T09:21:20+00:00","dateModified":"2021-12-04T04:46:24+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-map\/"},"wordCount":1270,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-map\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Maps-01-1.jpg","keywords":["Concatenating Maps in Scala","Declaring a Map with Values","Declaring a Scala Map","Introduction to Maps in Scala","Operations on a Map in Scala","Scala Map"],"articleSection":["Scala Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/scala-map\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/scala-map\/","url":"https:\/\/data-flair.training\/blogs\/scala-map\/","name":"Learn Scala Map with Examples Quickly &amp; Effectively - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scala-map\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scala-map\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Maps-01-1.jpg","datePublished":"2018-04-19T09:21:20+00:00","dateModified":"2021-12-04T04:46:24+00:00","description":"Scala Map: Learn to declare a Scala Map, Operations on a Map in Scala, Concatenating Maps, Printing Keys and Values from a Scala Map and much more...","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/scala-map\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/scala-map\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/scala-map\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Maps-01-1.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/04\/Scala-Collections-Maps-01-1.jpg","width":1200,"height":628,"caption":"Scala Map Map in Scala Declaring a Scala Map Operations on a Map in Scala Concatenating Maps in Scala"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/scala-map\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Scala Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/scala\/"},{"@type":"ListItem","position":3,"name":"Learn Scala Map with Examples Quickly &amp; Effectively"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=13992"}],"version-history":[{"count":8,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13992\/revisions"}],"predecessor-version":[{"id":104781,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/13992\/revisions\/104781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/34733"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=13992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=13992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=13992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}