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
Listclass (since 2.8.0, I think someone said), inherited from
toMap()which, if the sequence contains
Tuple2s, will make a
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!
Not sure if this will ever be of use to anyone else, but I thought I'd put it out there just in case.
You have a
List/Seqof keys and a
List/Seqof 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
The steps here are:
- "Zip" the keys list with the values list, which creates a
- Convert the list of
Tuples to an
Tuples. This is necessary because the
Mapobject, which extends
MapFactory, doesn't have any
apply()methods that deal with lists but only the one that accepts a var args parameter of
- Add this funky noise
": _*"which tells Scala to pass the
Arrayas multiple varargs parameters, rather than trying to pass the whole
Arrayas the first parameter.