Work with nested maps

546 views Asked by At

I have a nested map which i want to manipulate.

My data type is of the following:

nestedMap :SortedMap[Long, SortedMap[String, Double]]

I have an instance of nestedMap.

I want to create a List / SortedMap of the String (i.e. the key of the inner SortedMap).

For example:

val nestedMap: SortedMap[Long, SortedMap[String, Double]] = (1000L -> ("component1" -> 1.), 2000L -> ("component1" -> 1.1), 3000L -> ("component1" -> 0.95, "component2" -> 1.))

I want to create the following list:

component = List("component1", "component2") 

The number of components could be a couple of dozen elements, however the number of Long and Double elements could be thousands.

What is the most efficient way of doing this?

Thanks

1

There are 1 answers

0
Marth On BEST ANSWER
scala> nestedMap.values
                .flatMap(_.keys)
                .toSet   // or .toList.distinct if you want a List[String]
res3: scala.collection.immutable.Set[String] = Set(component1, component2)