我有两个 Java 类:LogEntry
和Record
.
该类LogEntry
具有如下所示的方法:
public LogEntry setRec(List<Map<String,List<Record>>> rec)
在 Scala 中,我尝试这样Record
输入:LogEntry
import scala.collection.JavaConversions._
import collection.mutable._
val log = new LogEntry()
val rec = new Record()
val map:java.util.Map[String, java.util.List[Record]] = HashMap("sessionKey" -> ArrayBuffer(rec))
log.setRec(List(map))
但我得到一个编译错误:
scala> val map:java.util.Map[String, java.util.List[Record]] = HashMap("sessionKey" -> ArrayBuffer(eventPart))
<console>:14: error: type mismatch;
found : scala.collection.mutable.HashMap[String,scala.collection.mutable.ArrayBuffer[Record]]
required: java.util.Map[String,java.util.List[Record]]
val map:java.util.Map[String, java.util.List[Record]] = HashMap("sessionKey" -> ArrayBuffer(eventPart))
^
似乎从 Scala 集合到 Java 集合的自动约定失败了,但如官方文档中所述:
mutable.Buffer <=> java.util.List
mutable.Map <=> java.util.Map
scala> val jul: java.util.List[Int] = ArrayBuffer(1, 2, 3)
jul: java.util.List[Int] = [1, 2, 3]
scala> val m: java.util.Map[String, Int] = HashMap("abc" -> 1, "hello" -> 2)
m: java.util.Map[String,Int] = {hello=2, abc=1}
ArrayBuffer
可以转换为java.util.List
,HashMap
也可以转换为java.util.Map
.
那么,为什么会出现这个错误呢?