Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala flatten nested map

I have a nested Map like this one:

Map(1 -> Map(2 -> 3.0, 4 -> 5.0), 6 -> Map(7 -> 8.0))

I would like to 'flatten' it in a way such that the keys of the outer and inner maps are paired, i.e. for the example above:

Seq((1,2),(1,4),(6,7))

what is an elegant way to do this?

like image 429
benroth Avatar asked Jan 18 '26 16:01

benroth


1 Answers

val m =  Map(1 -> Map(2 -> 3.0, 4 -> 5.0), 6 -> Map(7 -> 8.0))
m.toSeq.flatMap({case (k, v) => v.keys.map((k,_))})
like image 176
Lee Avatar answered Jan 21 '26 08:01

Lee