By Alvin Alexander. Last updated: August 2, 2022
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.
this post is sponsored by my books: | |||
#1 New Release |
FP Best Seller |
Learn Scala 3 |
Learn FP Fast |