0

I am trying to extract the values from the keys v1 and v2.

I would like it to print like this

first the v1

  • 900
  • 1800

and then v2

  • 700
  • 800
  • 2600
  • 1800
  • 2100

This is how far I have done but I get an error in my second for loop that I do not know how to solve. Is this write scala approach to solve this or is there a better way?

     val some_numbers = Map("v1"->(900, 1800),
                            "v2"->(700, 800, 2600, 1800, 2100))
     
     for ((key,values) <-some_numbers) {
       for(value<-values)
         print(value)
     }
2
  • Like this: scastie.scala-lang.org/BalmungSan/7e0qTtAaRAq2G8TFrGjlGg ? Commented Dec 30, 2021 at 17:48
  • 2
    It is very very unlikely that you really want to have a map with tuple values of different sizes. See the type of the map, it will be Map[String, Product], quite useless. You probably misunderstand the usage of tuples here. Commented Dec 30, 2021 at 19:18

1 Answer 1

2

You cannot iterate directly on a tuple value.

I do not know what is your application need. BTW you can iterate on tuple using productIterator:

 for ((key,values) <-some_numbers) {
   for(value<-values.productIterator) {
     println(value)
   }
 }

This should work. But probably it is better to use List instead of tuple in this case!

Sign up to request clarification or add additional context in comments.

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.