提问人:Arnianor 提问时间:3/20/2019 最后编辑:Arnianor 更新时间:3/20/2019 访问量:46
Scala:可变。Map[Any,可变。Map[Any, IndexedSeq[Any]] 问题
Scala: mutable.Map[Any, mutable.Map[Any, IndexedSeq[Any]] issue
问:
当我尝试将一个项目放在我的第二个可变地图中时,我遇到了一个小问题。 我的目标是:收集位于许多不同xml文件中的一些元素,并按照它们所属的层次结构来组织它们(这些文件是一个非结构化的混乱,类别似乎没有逻辑顺序)。 这些元素是:类别层次结构中的级别(1 - x,1 是顶层)作为 iLevel,类别代码作为 catCode,其名称,如果需要,其父级的名称(所有名称都位于 namesCategories 中)。
val categoryMap = mutable.Map.empty[Int, mutable.Map[String, IndexedSeq[String]]]
...
//Before: search in a first file links to other files
// for each category file found, will treat it and store it for further treatement.
matches.foreach{f =>
....
//Will search for a specific regex, and for each matches store what we are interested in
matchesCat.foreach{t =>
sCat = t.replaceFirst((system_env + """\S{4}"""), "")
//iLevel given by the number of '/' remaining in the string
iLevel = sCat.count(_ == '/')
//reset catCode and namesCategories
catCode = ""
namesCategories.clear()
//Search and extract the datas from sCat using premade regex patterns
sCat match {
case patternCatCode(codeCat) => catCode = s"$codeCat"
}
//remove the category code to prepare to extract names
sCat.replace(patternCatCode.toString(), "")
//extract names
do {
sCat match {
case patternCatNames(name) => namesCategories += s"$name"
}
sCat.replace(patternCatNames.toString(), "")
}while(sCat!="")
// create the level entry if it doesn't exist
if(!(categoryMap.contains(iLevel))) {
categoryMap.put(iLevel, mutable.Map.empty[String, IndexedSeq[String]])
}
//Try to add my cat code and the names, which must be in order for further treatment, to my map
categoryMap(iLevel).put(catCode, namesCategories.clone())
}
}
}
问题: 类型不匹配,预期:IndexedSeq[String],实际:可变。生成器[String, IndexedSeq[String]]
正如 Travis Brown 善意地指出的那样,我有一个类型不匹配的问题,但我不知道如何解决这个问题并让总体想法发挥作用。
我试图将代码保留为仅与此处相关的代码,如果需要更多内容,我会再次编辑。
有什么提示吗?
感谢您的帮助
答: 暂无答案
评论
IndexedSeq[String]
Builder
IndexedSeq.newBuilder