在scala中嵌套的映射迭代器

时间:2018-06-18 14:24:28

标签: scala scala-collections

我的代码中有嵌套地图输出,如下所示

gluLookAt(0,Y,0,    0,0,0,  0,0,1);

但我想使用scala迭代器输出如下所示,如果有人知道请帮助我,因为我是一个非常新的Scala

Map(test -> 113123, "cat" -> None, myList -> Map(test2 -> 321323, test3 -> 11122))

1 个答案:

答案 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)))

获得所需的输出

您可以使用printlnoutput视为

println(output)
//Map(test -> 113123, cat -> None, myList -> Some(Map(test2 -> 321323, test3 -> 11122)))