Typically I have been declaring a map in this fashion because I need it to be empty
var myMap: mutable.Map[String, ListBuffer[Objects]] = mutable.Map[String, ListBuffer[Objects]]()
The object type signature is long so I am trying to declare my own type alias as such in a package object like below.
type MyMapType = mutable.Map[String, ListBuffer[Objects]]
The problem is when I try to declare my map with the alias it doesn't seem to work.
var myMap: MyMapType = MyMapType()
I get an error saying
Expression of type ListBuffer[Objects] doesn't conform to expected type MyPackageObject.MyMapType
The emptyMap() method of Java Collections is a method that is used to return an empty map such that we can not change the data in map IE it is immutable.
The empty() method is utilized to make the map empty. Return Type: It returns an empty map.
Create Empty Map in Golang To create an empty Map in Go language, we can either use make() function with map type specified or use map initializer with no key:value pairs given.
Scala Map get() method with exampleThe get() method is utilized to give the value associated with the keys of the map. The values are returned here as an Option i.e, either in form of Some or None. Return Type: It returns the keys corresponding to the values given in the method as argument.
Try this:
import collection.mutable.{Map, ListBuffer}
type MyMapType = Map[String, ListBuffer[Object]]
var myMap: MyMapType = Map()
The reason why this works: Map()
is shortcut for Map.apply()
. It's a method invocation on an object called Map
. It is the companion object of mutable.Map
. Its name belongs to a completely separate set of identifiers: the names of types are independent from names of objects. Therefore, the companion object Map
is still called Map
. Note that you can omit all the type arguments to .apply()
because of the type inference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With