How to create an empty Scala Map

If you need to create an empty Map in Scala, the following examples show how to create empty immutable and mutable maps.

An empty immutable Map

You can use these approaches to create an empty immutable Scala Map:

val a = Map[Int, String]()
val a = Map.empty[Int, String]

Here’s what they look like in the REPL:

scala> val a = Map[Int, String]()
a: scala.collection.immutable.Map[Int,String] = Map()

scala> val a = Map.empty[Int, String]
a: scala.collection.immutable.Map[Int,String] = Map()

An empty mutable Map

You can use these approaches to create an empty mutable Scala Map:

import scala.collection.mutable.Map
val a = Map[Int, String]()
val a = Map.empty[Int, String]

Here’s what they look like in the REPL:

scala> import scala.collection.mutable.Map
import scala.collection.mutable.Map

scala> val a = Map[Int, String]()
a: scala.collection.mutable.Map[Int,String] = Map()

scala> val a = Map.empty[Int, String]
a: scala.collection.mutable.Map[Int,String] = Map()

If you needed to see how to create empty maps in Scala, I hope these examples are helpful.