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