Scala:可变。Map[Any,可变。Map[Any, IndexedSeq[Any]] 问题

Scala: mutable.Map[Any, mutable.Map[Any, IndexedSeq[Any]] issue

提问人:Arnianor 提问时间:3/20/2019 最后编辑:Arnianor 更新时间:3/20/2019 访问量:46

问:

当我尝试将一个项目放在我的第二个可变地图中时,我遇到了一个小问题。 我的目标是:收集位于许多不同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 善意地指出的那样,我有一个类型不匹配的问题,但我不知道如何解决这个问题并让总体想法发挥作用。

我试图将代码保留为仅与此处相关的代码,如果需要更多内容,我会再次编辑。

有什么提示吗?

感谢您的帮助

斯卡拉 可变

评论

0赞 user51 3/20/2019
你的问题不清楚你在问什么?什么是 iLevel、catCode 和 namesCategories?
4赞 Travis Brown 3/20/2019
编译器的消息对我来说似乎很清楚:你说你想要内部映射中的值,但你试图把你用 .IndexedSeq[String]BuilderIndexedSeq.newBuilder
0赞 Arnianor 3/20/2019
感谢你们俩。我进行了编辑,以便更准确地了解我在这里尝试实现的目标。我试图更改IndexedSeq.newBuilder,但后来我只是遇到了一些其他错误。欢迎更多意见。

答: 暂无答案