我正在尝试将这种MultiMap特性混合在一起,HashMap如下所示:
val children:MultiMap[Integer, TreeNode] =
new HashMap[Integer, Set[TreeNode]] with MultiMap[Integer, TreeNode]
Run Code Online (Sandbox Code Playgroud)
特征的定义MultiMap是:
trait MultiMap[A, B] extends Map[A, Set[B]]
Run Code Online (Sandbox Code Playgroud)
这意味着一种MultiMap类型A&B是一种Map类型A&Set[B],或者在我看来.但是,编译器抱怨:
C:\...\TestTreeDataModel.scala:87: error: illegal inheritance; template $anon inherits different type instances of trait Map: scala.collection.mutable.Map[Integer,scala.collection.mutable.Set[package.TreeNode]] and scala.collection.mutable.Map[Integer,Set[package.TreeNode]]
new HashMap[Integer, Set[TreeNode]] with MultiMap[Integer, TreeNode]
^ one error found
Run Code Online (Sandbox Code Playgroud)
仿制药似乎再次绊倒了我.
sbl*_*ndy 26
我不得不导入scala.collection.mutable.Set.似乎编译器认为Set in HashMap[Integer, Set[TreeNode]]是scala.collection.Set.MultiMap def中的Set是.scala.collection.mutable.Set
Cal*_*lum 12
这可能很烦人,Scala集合中的名称重载是它的一大弱点.
对于它的价值,如果您已经scala.collection._导入,您可能已经将您的HashMap类型写为:
new HashMap[ Integer, mutable.Set[ TreeNode ] ]
Run Code Online (Sandbox Code Playgroud)