我需要这样做:
{
“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" )如何通过遍历列表将键和值插入到映射中?我是gatling和scala的新手。
发布于 2015-09-24 03:25:34
在更详细地研究了您的问题后,我意识到您不仅仅是在问如何将集合转换为地图。结合来自How can I use map and receive an index as well in Scala?和Scala best way of turning a Collection into a Map-by-key?的答案,您可以执行以下操作:
List("first", "second", "third").zipWithIndex.map {
case (item, index) => ("item" + index) -> item.toString
}
>res0: List[(String, String)] = List((item0,first), (item1,second), (item2,third))https://stackoverflow.com/questions/32747231
复制相似问题