Explanation of Scala basic Map Mapping
This article mainly introduces "the explanation of Scala basic Map mapping". In the daily operation, I believe that many people have doubts about the explanation of Scala basic Map mapping. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Scala basic Map mapping explanation". Next, please follow the editor to study!
# # Map
A Scala map (Map) is an object with a set of key / value pairs. The key is unique in the mapping, but the value is not necessarily unique. Mapping is also known as a hash table. There are two kinds of mappings, immutable and mutable. By default, Scala uses immutable mapping (Map). If you want to use mutable collections (Map), you need to explicitly import the scala.collection.mutable.Map class
Val map = Map ("name"-> "xiaoming", "age"-> 20) println (map ("name"))
To declare an empty mapping, you cannot omit the type description, add a key-value pair to the mapping (Map), and use the operator +
Var m: Map [String,Int] = Map () m + = ("one"-> 1)
To determine whether a value is included in the map, you can use the contains method
If (map.contains ("age")) {println (map ("age"))}
If you need to create a variable mapping, you need to introduce the scala.collection.mutable.Map package, otherwise value update is not a member of scala.collection.immutable.Map [String,Any] map ("gender") = "cumputer"
Val map = Map ("name"-> "xiaoming", "age"-> 20) map ("gender") = "cumputer" println (map ("gender")
Traversing map
Val map = Map ("name"-> "xiaoming", "age"-> 20) map ("gender") = "cumputer" for ((kmemv)