0
我需要形成这样的:转换为从列表中映射加特林
{
“item” : “hardcoded_value”,
“item2” : “hardcoded_value”,
“item3” : “hardcoded_value”,
}
在EXEC块我想:
// list with items [“item1”, “ item2”, “ item3”]
val theList = session("itemNames").as[List[String]]
val theMap = Map.empty[String,String] // empty map
// add items from list in map
theList.foreach{ key =>
| theMap += key -> "hardcoded_value"
}
但得到错误的+ =位置。
也试过:
theList.foreach(key => theMap += key -> "hardcoded_value")
如何通过遍历一个列表中插入键和值到地图吗?我是加特林和斯卡拉的新手。