Scala - 如何将 Map[Int,List[List[IntVar]]] 转换为
Scala - How to convert Map[Int,List[List[IntVar]]] to
我是 Scala 编程的新手,flatmap 函数让我很头疼。
在你让我搜索之前,我对 Whosebug 做了一些研究:https://whosebug.com/search?q=scala+map+to+list 但我没有找到可以轻松解决我的问题的东西。
这是我的问题:
Jacop (http://jacopguide.osolpro.com/guideJaCoP.html) 的 Scala 编程项目,
我需要转换这种地图
Map[Int,List[List[IntVar]]]
至:
List[T]
// In my case , only a List[IntVar]
我知道我应该多次使用 flatMap 但我想知道在我的情况下如何正确使用它。
感谢您的帮助
如果您想要地图值中的每个 IntVar
,您可以这样做:
map.values.flatten.flatten.toList
对 values
returns 的调用 Iterable
包含地图的所有值。在本例中,它 returns 类型为 Iterable[List[List[IntVar]]]
的对象。对该对象的第一个 flatten
调用将其扁平化为 Iterable[List[IntVar]]
。第二个 flatten
调用将此对象进一步扁平化为 Iterable[IntVar]
。最后,toList
方法将其转换为 List[IntVar]
.
我是 Scala 编程的新手,flatmap 函数让我很头疼。
在你让我搜索之前,我对 Whosebug 做了一些研究:https://whosebug.com/search?q=scala+map+to+list 但我没有找到可以轻松解决我的问题的东西。
这是我的问题:
Jacop (http://jacopguide.osolpro.com/guideJaCoP.html) 的 Scala 编程项目,
我需要转换这种地图
Map[Int,List[List[IntVar]]]
至:
List[T]
// In my case , only a List[IntVar]
我知道我应该多次使用 flatMap 但我想知道在我的情况下如何正确使用它。
感谢您的帮助
如果您想要地图值中的每个 IntVar
,您可以这样做:
map.values.flatten.flatten.toList
对 values
returns 的调用 Iterable
包含地图的所有值。在本例中,它 returns 类型为 Iterable[List[List[IntVar]]]
的对象。对该对象的第一个 flatten
调用将其扁平化为 Iterable[List[IntVar]]
。第二个 flatten
调用将此对象进一步扁平化为 Iterable[IntVar]
。最后,toList
方法将其转换为 List[IntVar]
.