Scala 可变map与不可变map互转

object test2 {
def main(args: Array[String]): Unit = {
val im = scala.collection.immutable.Map(1->"one",2->"two") //不可变map
val imh = scala.collection.immutable.HashMap(1->"one",2->"two") //不可变hashmap
val mu = scala.collection.mutable.Map(1->"one",2->"two") //可变map
println(im,manOf(im))
println(imh,manOf(imh))
println(mu,manOf(mu))

//mutable.Map to immutable.Map
val mui = mu.toMap
println(mui,manOf(mui))

//immutable.Map to mutable.Map
val immu = scala.collection.mutable.Map(im.toSeq:_*)
println(immu,manOf(immu))
val imhmu = scala.collection.mutable.Map(imh.toSeq:_*)
println(imhmu,manOf(imhmu))

}

//查看数据类型方法-manOf(data)
def manOf[T:Manifest](t:T):Manifest[T]=manifest[T]
}

注意:

-.toSeq:将元组List转换成mutable.Map

-:_*:表示变长参数

posted @ 2019-10-25 17:23  糖醋排骨加辣椒  阅读(6039)  评论(0编辑  收藏  举报