我的代码中有嵌套地图输出,如下所示
gluLookAt(0,Y,0, 0,0,0, 0,0,1);
但我想使用scala迭代器输出如下所示,如果有人知道请帮助我,因为我是一个非常新的Scala
Map(test -> 113123, "cat" -> None, myList -> Map(test2 -> 321323, test3 -> 11122))
答案 0 :(得分:1)
假设您有
之类的数据val data = Map("test" -> 113123, "cat" -> None, "myList" -> Map("test2" -> 321323, "test3" -> 11122))
//data: scala.collection.immutable.Map[String,Any] = Map(test -> 113123, cat -> None, myList -> Map(test2 -> 321323, test3 -> 11122))
然后你可以做
val output = data.map(x => if (x._2.isInstanceOf[Map[String, Long]]) (x._1 -> Some(x._2)) else x)
//output: scala.collection.immutable.Map[String,Any] = Map(test -> 113123, cat -> None, myList -> Some(Map(test2 -> 321323, test3 -> 11122)))
获得所需的输出
您可以使用println
将output
视为
println(output)
//Map(test -> 113123, cat -> None, myList -> Some(Map(test2 -> 321323, test3 -> 11122)))