Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala中的SynchronizedSet和set操作_Scala_Set_Hashset_Synchronized - Fatal编程技术网

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。