Scala中的SynchronizedSet和set操作
答复:Scala中的SynchronizedSet和set操作,scala,set,hashset,synchronized,Scala,Set,Hashset,Synchronized,答复: import collection.mutable.{ HashSet, SynchronizedSet } var myPool = new HashSet[String] with SynchronizedSet[String] myPool += "oh" myPool += "yes" myPool = myPool.tail 我得到: error: type mismatch; found : scala.collection.mutable.HashSet[Stri
import collection.mutable.{ HashSet, SynchronizedSet }
var myPool = new HashSet[String] with SynchronizedSet[String]
myPool += "oh"
myPool += "yes"
myPool = myPool.tail
我得到:
error: type mismatch;
found : scala.collection.mutable.HashSet[String]
required: scala.collection.mutable.HashSet[String] with scala.collection.mutable.SynchronizedSet[String]
myPool = myPool.tail
^
我做错了什么?正如消息所说,
myPool.tail
的类型是HashSet[String]
,并且您的变量myPool
被声明为HashSet[String]和SynchronizedSet[String]
您只需要声明想要避免过于精确的推断类型
var myPool : HashSet[String] = new HashSet[String] with SynchronizedSet[String]
请注意,在可变集合上,tail
是一个代价高昂的操作,它会返回一个新集合。那可能不是你想要的。(此外,规格对于要移除的元件是静音的)注意,tail
将不同步。它将是一个新集合,实例化时没有SynchronizedSet
trait。