当我在初始化时设置不可变的hashmap内容时:
var result_tags=HashMap[String,Int]()
result_tags=("video"->0,"game"->0,"news"->0,"ee"->0,"sport"->0,
"shop"->0,"ju"->0,"story"->0,"pho"->0,"con"->0,"live"->0,"life"->0,"soft"->0,"hire"->0,"car"->0,
"mm"->0,"mus"->0,"mob"->0,"male"->0,"heal"->0, "sca"->0,"bank"->0,"mail"->0,"cool"->0,"pict"->0, "dl"->0)
它给了我错误:
too many elements for tuple:26,allowed:22
这意味着元组的最大数量为22.我知道->
用于创建元组。是否有其他方法来初始化hashmap而没有其元素的限制数。
答案 0 :(得分:7)
您实际在做的是初始化巨型Tuple
类型并尝试将其分配给类型为result_tags
的{{1}}变量,即使元组大小不会超过
最大尺寸。
因此,关于元组的错误并不是指您使用的HashMap
语法,而是指->
列表中的元素数。即使你这样写,你也会得到同样的错误:
(...)
其次,在你的情况下你应该这样做:
(("video", 0), ("game", 0), ..., ("dl", 0))
(注意我省略了类型信息,因为Scala会为你推断出类型。)
var result_tags = HashMap("video" -> 0, "game" -> 0, ..., "dl" -> 0)
语法在Scala中完全不同,因为它初始化了一个元组类型。每个此类声明都转换为(a1, a2, ..., aN)
类型,其中最大大小为22.因此,Scala库实际上有22个不同的TupleN
类,从Tuple
到Tuple1
。
第三,你的风格可以使用一些修正
Tuple22
scala.collection.Map
而不是val
var
答案 1 :(得分:1)
只是
var result_tags = Map("video"->0,"game"->0,"news"->0,"ee"->0,"sport"->0,
"shop"->0,"ju"->0,"story"->0,"pho"->0,"con"->0,"live"->0,"life"->0,"soft"->0,"hire"->0,"car"->0,
"mm"->0,"mus"->0,"mob"->0,"male"->0,"heal"->0, "sca"->0,"bank"->0,"mail"->0,"cool"->0,"pict"->0, "dl"->0)
答案 2 :(得分:0)
这是在Scala中初始化Map的方法
地图初始化
import scala.collection.immutable.Map
val result_tags = Map("video" -> 0, "game" -> 0, "news" -> 0, "ee" -> 0, "sport" -> 0,
"shop" -> 0, "ju" -> 0, "story" -> 0, "pho" -> 0, "con" -> 0, "live" -> 0, "life" -> 0, "soft" -> 0, "hire" -> 0, "car" -> 0,
"mm" -> 0, "mus" -> 0, "mob" -> 0, "male" -> 0, "heal" -> 0, "sca" -> 0, "bank" -> 0, "mail" -> 0, "cool" -> 0, "pict" -> 0, "dl" -> 0)
HashMap初始化
import scala.collection.immutable.HashMap
val result_tags = HashMap("video" -> 0, "game" -> 0, "news" -> 0, "ee" -> 0, "sport" -> 0,
"shop" -> 0, "ju" -> 0, "story" -> 0, "pho" -> 0, "con" -> 0, "live" -> 0, "life" -> 0, "soft" -> 0, "hire" -> 0, "car" -> 0,
"mm" -> 0, "mus" -> 0, "mob" -> 0, "male" -> 0, "heal" -> 0, "sca" -> 0, "bank" -> 0, "mail" -> 0, "cool" -> 0, "pict" -> 0, "dl" -> 0)
答案 3 :(得分:0)
您还可以使用immutable.HashMap类中的var myDiv = document.createElement("div");
var imgPath = "http://127.0.0.1:8888/service/file/download/hd1.jpg?code=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU3ZTYwOTMyOWE5MmQ0MGEwNzk5YzZmYiJ9.H78qRMZNOfWxVnA8mbybQD5Yh1ZBVRhRQgzFNmJMVvs";
$(myDiv).css("background-image", "url(" + imagPath + ")");
方法来实现您尝试执行的操作:
来自scala doc:
+