Since I posted this 24 hours ago, no fewer than 13 lovely people have left comments to let me know that this is actually far, far easier than I thought. There is a function on the
List
class (since 2.8.0, I think someone said), inherited from TraversableOnce
, called toMap()
which, if the sequence contains Tuple2
s, will make a Map
from them.So the ideal solution is simply:
(keys zip values) toMapNow the question is: How come everyone else knows about this and I didn't? :(
A couple of people also mentioned that you don't need to convert a List into an Array to be able to pass it into a var args method (but you still need the funky noise). Handy!
Thanks, everyone.
Original Post...
Not sure if this will ever be of use to anyone else, but I thought I'd put it out there just in case.
Problem:
You have a
List/Seq
of keys and a List/Seq
of values, with the key at index n in one list matching the value at index n in the other. But what you really want is to convert them into a Map
.Solution:
Map(keys.zip(values).toArray: _*)
Explanation:
The steps here are:
- "Zip" the keys list with the values list, which creates a
List
ofTuple2
objects - Convert the list of
Tuple
s to anArray
ofTuple
s. This is necessary because theMap
object, which extendsMapFactory
, doesn't have anyapply()
methods that deal with lists but only the one that accepts a var args parameter ofTuple2
s. - Add this funky noise
": _*"
which tells Scala to pass theArray
as multiple varargs parameters, rather than trying to pass the wholeArray
as the first parameter.
Hello Graham,
ReplyDeletethanks for the post. It shows how terse and elegant the Scala language is.
It's not exactly in the topic, but, trying to understand the code I read the ScalaDoc for the zip(), that signs:
def zip[A1 >: A,B,That](that: Iterable[B])(implicit bf: scala.collection.generic.CanBuildFrom[Repr,(A1, B),That]): That
would you please help us to read/understand this?
Thank you.
Hi Marcos,
ReplyDeleteI've created a blog post about Scala's CanBuildFrom class in response to your question.
I hope it answers what you're looking for.
Cheers,
Graham.
It looks much easier to convert Map into List in Java. Thanks for sharing this nice tip.
ReplyDelete