0
votes

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

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

1 Answers

1
votes

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

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