如何从Buffer [Map [String,String]]转换为Map [String,String]

时间:2015-11-18 13:49:54

标签: scala

以下代码会引发以下错误: 类型不匹配; found:scala.collection.mutable.Buffer [scala.collection.immutable.Map [String,String]] required:Map [String,String]

def getRecordsByRegExp(table: String, family: String, regExp: String): Future[Seq[SomeClass]] = {
    val scanner = hBaseClient.newScanner(table)

    scanner.setFamily(family)
    scanner.setFilter(new KeyRegexpFilter(regExp))

    scanner.nextRows.map(arrayList =>
      arrayList.flatten.map(x =>
        Record(
          table,
          new String(x.key),
          arrayList.map(keyValues =>
            keyValues.map(kv => new String(kv.qualifier) -> new String(kv.value)).toMap))))

  }

SomeClass定义: SomeClass(table:String,key:String,values:Map [String,String])

有关如何克服这个问题的任何提示?

稍后编辑:添加了所有代码

2 个答案:

答案 0 :(得分:0)

如果我理解正确,那么这可以解决您的问题:

scanner.nextRows.map{
  arrayList =>
    val map = arrayList.flatten.map{
      kv =>
        new String(kv.qualifier) -> new String(kv.value)
    }.toMap
    arrayList.flatten.map{
      x =>
      SomeClass(
        table,
        new String(x.key),
        map)
    }
}

答案 1 :(得分:0)

感谢Till,我也使用了它:

scanner.nextRows.map(arrayList =>
      arrayList.flatten.map(x =>
        SomeClass(
          table,
          new String(x.key),
          Map(new String(x.qualifier) -> new String(x.value)))))