我有以下代码
object DispatchLibrary
{
private var nodes = Map.empty[java.util.UUID, List[BigInt]]
def addNode(uuid: java.util.UUID) = if(nodes contains uuid) nodes else (nodes += (uuid -> Nil))
def addValue(uuid: java.util.UUID, value: BigInt) = nodes + (uuid -> (value :: (nodes get uuid getOrElse Nil)))
//def getValue(uuid: java.util.UUID) : List[BigInt] = ???
//def getValues() : List[BigInt] = ???
def calculated(): Boolean = !nodes.exists(_._1 eq null)
def main(args: Array[String]) : Unit =
{
val uuid = java.util.UUID.randomUUID()
addNode(uuid)
addValue(uuid, BigInt(999))
addValue(uuid, BigInt(9999))
nodes foreach {case (key, value) => println (key + "->" + value)}
}
}
在 IntelliJ IDEA 中运行上述代码会得到类似于以下输出的内容
8b2b4a7b-3e65-4de0-9035-8ee1d2910983->List()
我不确定为什么没有打印列表。
从 REPL 运行类似的代码会给出预期的输出
scala> var nodes = Map.empty[Int, List[BigInt]]
nodes: scala.collection.immutable.Map[Int,List[BigInt]] = Map()
scala> nodes += (1->Nil)
scala> nodes += (1 -> (BigInt(999) :: (nodes get 1 getOrElse Nil)))
scala> nodes += (1 -> (BigInt(9999) :: (nodes get 1 getOrElse Nil)))
scala> nodes foreach {case (key, value) => println (key + "-->" + value )}
1-->List(9999, 999)
如果您能帮助我编写评论的方法,我也将不胜感激。