Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala:使用两个列表创建HashMap_Scala_List_Functional Programming_Hashmap - Fatal编程技术网

Scala:使用两个列表创建HashMap

Scala:使用两个列表创建HashMap,scala,list,functional-programming,hashmap,Scala,List,Functional Programming,Hashmap,我在Scala中有两个列表: val workersList = Worker1 :: Worker2 :: Worker3 :: Worker4 :: Nil // type List[Worker] val workStationsList = WS1 :: WS2 :: WS3 :: WS4 :: Nil // type List[WorkStation] 辅助对象有一个参数worksIn:List[工作站] 知道Worker1在WS1和WS2中工作,Worker2在WS1和WS2中工作

我在Scala中有两个列表:

val workersList = Worker1 :: Worker2 :: Worker3 :: Worker4 :: Nil // type List[Worker]
val workStationsList = WS1 :: WS2 :: WS3 :: WS4 :: Nil // type List[WorkStation]
辅助对象有一个参数
worksIn:List[工作站]

知道
Worker1
WS1
WS2
中工作,
Worker2
WS1
WS2
中工作,
Worker3
WS3
WS4
WS3
WS4
中工作,我想得到一个
HashMap[工人,列表[工作站]
这正好说明了这一点

结果应该是这样的:

Worker1 -> List(WS1 :: WS2 :: Nil)
Worker2 -> List(WS1 :: WS2 :: Nil)
Worker3 -> List(WS3 :: WS4 :: Nil)
Worker4 -> List(WS3 :: WS4 :: Nil)
我试过这么做,但没用:

val list = workersList.flatMap(w => workStationsList.map(ws => if(w.worksIn.contains(w)) w -> ws)).toMap[Worker, List[WorkStation]]
有人知道我怎么做吗

val list = workersList.map(w => (w -> w.worksIn)).toMap // type Map[Worker, List[WorkStation]]

workStationList
似乎是多余的。

真正问题的可能重复之处不是如何将两个列表转换为哈希映射。这就是我如何得到我想要的结果。这个答案对我的具体问题没有帮助。不确定你需要第二个列表做什么,你可以简单地做
workersList.map(w=>(w,w.worksIn)).toMap