scala hashmap
Let's first understand what are maps and hashmaps?
首先让我们了解什么是map和hashmap ?
map in Scala is a collection that stores its elements as key-value pairs, like a dictionary.
Scala中的map是一个集合,将其元素存储为键值对,例如字典。
Example:
例:
Map( 1 -> Scala, 2 -> Python, 3 -> Javascript)
hashmap is a collection based on maps and hashes. It stores key-value pairs.
hashmap是基于映射和哈希的集合。 它存储键值对。
Example:
例:
HashMap( 1 -> Scala, 2 -> Python, 3 -> Javascript)
将HashMap转换为Map (Converting HashMap to Map)
In Scala, we can convert a hashmap to a map using the tomap method.
在Scala中,我们可以使用tomap方法将哈希图转换为地图。
Syntax:
句法:
Map = HashMap.toMap
Scala program to convert hashmap to map
Scala程序将hashmap转换为map
import scala.collection.mutable.HashMap ;
object MyClass {
def main(args: Array[String]) {
val hashMap = HashMap(1->"Scala", 2->"Python", 3->"JavaScript")
println("HashMap: " + hashMap)
val map = hashMap.toMap
println("Map: " + map)
}
}
Output:
输出:
HashMap: HashMap(1 -> Scala, 2 -> Python, 3 -> JavaScript)
Map: Map(1 -> Scala, 2 -> Python, 3 -> JavaScript)
Explanation:
说明:
In the above code, we have created a HashMap and then convert it to a Map using the toMap method.
在上面的代码中,我们创建了一个HashMap,然后使用toMap方法将其转换为Map。
翻译自: https://www.includehelp.com/scala/convert-hashmap-to-map.aspx
scala hashmap