ScalaDoc关于 concurrentMap 的说法是:“已弃用(自 2.10.0 版起)请改用 scala.collection.concurrent.Map
。”不幸的是,rest of the Scala docs尚未更新,仍然引用 concurrentMap
.
我试图混入concurrent.Map
变成 HashMap
,结果如下:
scala> val mmap = new mutable.HashMap[String, String] with collection.concurrent.Map[String, String]
<console>:16: error: object creation impossible, since:
it has 4 unimplemented members.
/** As seen from anonymous class $anon, the missing signatures are as follows.
* For convenience, these are usable as stub implementations.
*/
def putIfAbsent(k: String,v: String): Option[String] = ???
def remove(k: String,v: String): Boolean = ???
def replace(k: String,v: String): Option[String] = ???
def replace(k: String,oldvalue: String,newvalue: String): Boolean = ???
val mmap = new mutable.HashMap[String, String] with collection.concurrent.Map[String, String]
所以我们看到,除了简单的 mixin,还必须实现一些方法。这是最好的使用方式吗
concurrent.Map
, 或者,还有更好的方法?
最佳答案
直接取自此评论:
https://stackoverflow.com/a/49689669/7082628
在 2018 年,显然你可以这样做:
import java.util.concurrent.ConcurrentHashMap
val m: ConcurrentHashMap[String,MyClass] = new ConcurrentHashMap
关于scala - 在 Scala concurrent.Map 中混合的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18660769/