简体   繁体   English

Scala中的嵌套地图迭代器

[英]Nested map iterator in scala

I'm have Nested Map output in my code like below 我在我的代码中有嵌套地图输出,如下所示

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

But I wanted output like below using scala iterator if anyone knows please help me on this as I'm very new Scala 但是我想像下面这样使用scala迭代器进行输出,如果有人知道,请帮助我,因为我是非常新的Scala

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

Supposing that you have data like 假设您有类似的数据

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

Then you can do 那你可以做

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

to get your desired output 得到你想要的输出

And you can use println to see the output as 您可以使用printlnoutput视为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM