如何在 Gatling Feeder 中传递 java HashMap
How to pass java HashMap in Gatling Feeder
我正在尝试调用 Map.canBuildFrom(("dataLine", dataLine));其中数据行是一个HashMap,我想传递hashMap,我该怎么做?
val myCustomFeeder = new Feeder[String] {
override def hasNext = true
override def next: Map[String, String] = {
var dataLine: java.util.HashMap[String, String] = new RandomDataGenerator().getMessageMap(); // this returns a java.util.HashMap
Map.canBuildFrom(("dataLine", dataLine)); // does not compile.
}
}
我得到的错误是:
Error:(91, 11) overloaded method value apply with alternatives:
()scala.collection.mutable.Builder[(A, B),scala.collection.immutable.Map[A,B]] <and>
(from: Map.Coll)scala.collection.mutable.Builder[(A, B),scala.collection.immutable.Map[A,B]]
cannot be applied to ((String, java.util.HashMap[String,String]))
Map.canBuildFrom(("dataLine", dataLine));
^
正如@Łukasz 所建议的那样,使用 JavaConversions
:
def next: Map[String, String] = {
var dataLine: java.util.HashMap[String, String] = ???
scala.collection.JavaConversions.mapAsScalaMap(dataLine).toMap
}
或者,使用隐式:
def next: Map[String, String] = {
var dataLine: java.util.HashMap[String, String] = ???
import scala.collection.JavaConversions._
dataLine.toMap
}
这里唯一的缺点是,因为您需要 return 不可变 Map
,您必须调用 toMap
,这基本上会复制整个地图。
mapAsScalaMap
returns 是 Java 映射的包装器,与复制相比,这是一个廉价的操作,所以如果你能这样做,你会有更好的性能:
def next: scala.collection.mutable.Map[String, String] = {
var dataLine: java.util.HashMap[String, String] = ???
import scala.collection.JavaConversions._
dataLine
}
我正在尝试调用 Map.canBuildFrom(("dataLine", dataLine));其中数据行是一个HashMap,我想传递hashMap,我该怎么做?
val myCustomFeeder = new Feeder[String] {
override def hasNext = true
override def next: Map[String, String] = {
var dataLine: java.util.HashMap[String, String] = new RandomDataGenerator().getMessageMap(); // this returns a java.util.HashMap
Map.canBuildFrom(("dataLine", dataLine)); // does not compile.
}
}
我得到的错误是:
Error:(91, 11) overloaded method value apply with alternatives:
()scala.collection.mutable.Builder[(A, B),scala.collection.immutable.Map[A,B]] <and>
(from: Map.Coll)scala.collection.mutable.Builder[(A, B),scala.collection.immutable.Map[A,B]]
cannot be applied to ((String, java.util.HashMap[String,String]))
Map.canBuildFrom(("dataLine", dataLine));
^
正如@Łukasz 所建议的那样,使用 JavaConversions
:
def next: Map[String, String] = {
var dataLine: java.util.HashMap[String, String] = ???
scala.collection.JavaConversions.mapAsScalaMap(dataLine).toMap
}
或者,使用隐式:
def next: Map[String, String] = {
var dataLine: java.util.HashMap[String, String] = ???
import scala.collection.JavaConversions._
dataLine.toMap
}
这里唯一的缺点是,因为您需要 return 不可变 Map
,您必须调用 toMap
,这基本上会复制整个地图。
mapAsScalaMap
returns 是 Java 映射的包装器,与复制相比,这是一个廉价的操作,所以如果你能这样做,你会有更好的性能:
def next: scala.collection.mutable.Map[String, String] = {
var dataLine: java.util.HashMap[String, String] = ???
import scala.collection.JavaConversions._
dataLine
}