문제

섞어보려고 하는데 MultiMap 을 가진 특성 HashMap 이렇게:

val children:MultiMap[Integer, TreeNode] = 
    new HashMap[Integer, Set[TreeNode]] with MultiMap[Integer, TreeNode]

에 대한 정의 MultiMap 특성은 다음과 같습니다

trait MultiMap[A, B] extends Map[A, Set[B]]

의미하는 것은 MultiMap 종류의 A & BMap 종류의 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  

제네릭이 나를 다시 넘어지게하는 것 같습니다.

도움이 되었습니까?

해결책

수입해야 했는데요 scala.collection.mutable.Set.컴파일러가 Set in을 생각한 것 같습니다. HashMap[Integer, Set[TreeNode]] ~였다 scala.collection.Set.MultiMap 정의의 Set은 다음과 같습니다. scala.collection.mutable.Set.

다른 팁

Scala 컬렉션에 이름이 오버로드되는 것은 Scala의 큰 약점 중 하나입니다.

그만한 가치가 있다면, 만약 당신이 그랬다면 scala.collection._ 가져온 경우 아마도 귀하는 귀하의 HashMap 다음과 같이 입력하세요:

new HashMap[ Integer, mutable.Set[ TreeNode ] ]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top