如何遍历 <String,Any> 类型的 MultiValueMap,其中 <Any> 可以是另一个 MultiValueMap 等等

How to iterate over MultiValueMap of type <String,Any> where <Any> can be another MultiValueMap and so on

我想遍历 MultiValueMap 类型 <String,Any> ,其中 Any 可以是另一个 MultiValueMap 类型 <String,Any> 并且 Any 可以是另一个 MultiValeMap 等等.我的代码是只提取地图的第一层:-("result" 变量是 MultiValueMap

val entrySet = result.entrySet();
val it = entrySet.iterator();
//System.out.println("  Object key  Object value");
while (it.hasNext()) {
    val mapEntry= it.next().asInstanceOf[java.util.Map.Entry[String,Any]];
    val list = (result.get(mapEntry.getKey()).asInstanceOf[List[String]])
    for (j <- 0 to list.size - 1) {
        //mapEntry.setValue("dhjgdj")
        System.out.println("\t" + mapEntry.getKey() + "\t  " + list.get(j));
    }
}

一种方法是收集所有元素(键->值对),然后将累积的集合变成迭代器。

def toItr(m: Map[String,_]): Iterator[(String,_)] =
  m.foldLeft(Vector.empty[(String,_)]){
    case (acc, (k, v: Map[String,_])) => acc ++ toItr(v).toVector
    case (acc, x) => acc :+ x
  }.toIterator

toItr( Map("a"->1, "b"->3, "c"->Map("x"->11, "y"->22)) )
// result: Iterator[Tuple2[String, _]] = non-empty iterator
// contents: (a,1), (b,3), (x,11), (y,22)