200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > scala hashmap_如何在Scala中将Hashmap转换为Map?

scala hashmap_如何在Scala中将Hashmap转换为Map?

时间:2019-03-04 15:56:43

相关推荐

scala hashmap_如何在Scala中将Hashmap转换为Map?

scala hashmap

Let's first understand what aremapsandhashmaps?

首先让我们了解什么是maphashmap

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 thetomapmethod.

在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.toMapprintln("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 thetoMapmethod.

在上面的代码中,我们创建了一个HashMap,然后使用toMap方法将其转换为Map。

翻译自: /scala/convert-hashmap-to-map.aspx

scala hashmap

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。