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

scala hashmap

Let's first understand what are maps and hashmaps?

首先让我们了解什么是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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值