Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala, is there a way to take convert two lists into a Map?

I have a two lists, a List[A] and a List[B]. What I want is a Map[A,B] but I want the semantics of zip. So started out like so:

var tuplesOfAB = listOfA zip listOfB 

Now I'm not sure how to construct a Map from my tuplesOfAB.

As a follow-up question, I also want to invert my map so that from a Map[A,B] I can create a Map[B,A]. Can anyone hit me with a clue-stick?

like image 493
Andy Hull Avatar asked Feb 03 '10 04:02

Andy Hull


People also ask

How do I convert a list to map in Scala?

To convert a list into a map in Scala, we use the toMap method. We must remember that a map contains a pair of values, i.e., key-value pair, whereas a list contains only single values. So we have two ways to do so: Using the zipWithIndex method to add indices as the keys to the list.

How do you make a map in Scala?

Map class explicitly. If you want to use both mutable and immutable Maps in the same, then you can continue to refer to the immutable Map as Map but you can refer to the mutable set as mutable. Map. While defining empty map, the type annotation is necessary as the system needs to assign a concrete type to variable.

Is Scala map a HashMap?

Scala HashMap is used to store objects and it take the object in the form of key value pair. For every value there should be one key associated with it. Scala collection contains this Hashmap and it is the implementation of MAP.

What is Scala map function?

Advertisements. map() method is a member of TraversableLike trait, it is used to run a predicate method on each elements of a collection. It returns a new collection.


2 Answers

In 2.8 this is really simple using the CanBuildFrom functionality (as described by Daniel) and using breakOut with a type instruction to the compiler as to what the result type should be:

import scala.collection.breakOut val m = (listA zip listB)(breakOut): Map[A,B] 

The following would also work:

val n: Map[A,B] = (listA zip listB)(breakOut) 

And (as EastSun, below, has pointed out) this has been added to the library as toMap

val o = (listA zip listB).toMap 

As for reversing the map, you can do:

val r = m.map(_.swap)(breakOut): Map[B, A] 
like image 97
oxbow_lakes Avatar answered Oct 05 '22 19:10

oxbow_lakes


Now that you've got a list of tuples it is easy to make it into a map by writing Map(tuplesOfAB: _*). The : _* notation means to call the varargs overload with the arguments taken from the sequence. This seems like a funny bit of syntax, but it helps to think that varargs are declared like Map[A,B](pairs: (A,B)*) and the : _* is a type annotation to convert to varargs because of the common * part.

To reverse a map m use Map(m.map(_.swap): _*). In scala a map is also a collection of pairs. This transforms those pairs by swapping the elements and passing them to the Map constructor.

like image 37
Geoff Reedy Avatar answered Oct 05 '22 18:10

Geoff Reedy