I have a Map[String,Seq[String]] and want to basically covert it to a Map[String,String] since I know the sequence will only have one value.
I have a Map[String,Seq[String]] and want to basically covert it to a Map[String,String] since
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Someone else already mentioned
mapValues, but if I were you I would do it like this:Two reasons:
The
mapValuesmethod produces a view of the result Map, meaning that the function will be recomputed every time you access an element. Unless you plan on accessing each element exactly once, or you only plan on accessing a very small percentage of them, you don’t want that recomputation to take place.Using a case with
(k,Seq(v))ensures that an exception will be thrown if the function ever sees a Seq that doesn’t contain exactly one element. Using_(0)or_.headwill throw an exception if there are zero elements, but will not complain if you had more than one, which will likely result in mysterious bugs later on when things go missing without errors.