scala中的observablemap

sca*_*ner 1 scala scala-collections

我是斯卡拉的新手.我想在修改地图时收到通知.我认为这可以使用可观察的地图来完成.

我试图定义一个像下面的对象

var myObj = new Map[UUID, MyType] with ObservableMap[UUID,MyType]
Run Code Online (Sandbox Code Playgroud)

但它没有编译说..

error: object creation impossible, since:
method iterator in trait MapLike of type => Iterator[(java.util.UUID, MyType)] is not defined
method get in trait MapLike of type (key: java.util.UUID)Option[MyType] is not defined
method -= in trait ObservableMap of type (key: java.util.UUID)this.type is marked `abstract' and `override', but no concrete implementation could be found in a base class
method += in trait ObservableMap of type (kv: (java.util.UUID, MyType))this.type is marked `abstract' and `override', but no concrete implementation could be found in a base class
Run Code Online (Sandbox Code Playgroud)

为什么会这样?如何实例化ObservableMap?

Gar*_*owe 5

您需要混合ObservableMap使用具体的地图类型.

scala> import scala.collection.mutable._
import scala.collection.mutable._

scala> val map = new HashMap[Int, Int] with ObservableMap[Int, Int]
map: scala.collection.mutable.HashMap[Int,Int] with scala.collection.mutable.ObservableMap[Int,Int] = Map()
Run Code Online (Sandbox Code Playgroud)