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

129 Views Asked by At

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 : https://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

1

There are 1 best solutions below

1
On BEST 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].