0

I was thinking of a way to create a tuple consisting of the String key from the map along with each of the Strings from the Set together as tuple that form the key in a new map. Value for the new map will be initialized to 0.0.

Ex: If I have to following:

Map[ USA, Set[CA, NY, WA]]

I want to create a new map from this which looks like:

Map[(USA,CA) -> 0.0, (USA,NY) -> 0.0, (USA,WA) -> 0.0]

I am able to create a Map[String, String] but I was hoping to get some help in creating the tuple key.

2 Answers 2

4
Map("USA" -> Set("CA", "NY", "WA")) flatMap { case (k, set) => set.map((k, _) -> 0.0) }
Sign up to request clarification or add additional context in comments.

Comments

1
val myMap = Map("USA" -> Set("CA", "NY", "WA"))

val newMap = myMap.foldLeft(Map[(String, String), Double]()) {
  case (acc, (key, values)) => {
    acc ++ (for {
      value <- values
    } yield (key, value) -> 0.0)
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.