【问题标题】:Convert to Map from list in Gatling从加特林列表转换为地图
【发布时间】:2015-09-24 03:25:33
【问题描述】:

我需要这样形成:

{
“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 的新手。

【问题讨论】:

    标签: scala scala-collections gatling


    【解决方案1】:

    在更详细地查看了您的问题后,我意识到您不仅仅是在询问将 Collection 转换为 Map 的问题。结合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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多