jacques y jacques y - 9 days ago 6
Scala Question

Scala - How to convert Map[Int,List[List[IntVar]]] to

I am new in Scala programming and the flatmap function gives me headache.

Before you ask me to search, I made some research on Stackoverflow : http://stackoverflow.com/search?q=scala+map+to+list but I didn't find something to solve easily my problem.

Here is my problem :

For a scala programming project with Jacop (http://jacopguide.osolpro.com/guideJaCoP.html) ,

I need to convert this kind of map

Map[Int,List[List[IntVar]]]


to :

List[T]
// In my case , only a List[IntVar]


I know I should use several times flatMap but I wonder how correctly use that in my case.

Thanks for your help

Answer

If you want every IntVar from the values of the map, you could to this:

 map.values.flatten.flatten.toList

The call to values returns an Iterable containing all the values of the map. In this case, it returns an object of type Iterable[List[List[IntVar]]]. The first flatten-call on this object flattens it to an Iterable[List[IntVar]]. The second flatten-call flattens this object further to an Iterable[IntVar]. Finally, the toList method converts it to a List[IntVar].