Best practices for mixing in Scala concurrent.Map

后端 未结 5 2118
温柔的废话
温柔的废话 2021-02-01 03:37

The ScalaDoc says this about concurrentMap: \"Deprecated (Since version 2.10.0) Use scala.collection.concurrent.Map instead.\" Unfortunately, the rest of the Scala

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-01 04:07

    By "simple mixin", perhaps you're asking if the trait can be used as a decorator as shown here for SynchronizedMap, and the answer is apparently not.

    Implementations include TrieMap and the wrapper for Java's ConcurrentMap (of which there are two implementations). (Java also offers ConcurrentSkipListSet as Set.)

    Also see this roll-your-own question.

    They have you covered on the conversion side of things, if that's what you were used to:

    scala> import java.util.concurrent._
    import java.util.concurrent._
    
    scala> import collection.JavaConverters._
    import collection.JavaConverters._
    
    scala> val m = new ConcurrentHashMap[String, Int]
    m: java.util.concurrent.ConcurrentHashMap[String,Int] = {}
    
    scala> val mm = m.asScala
    mm: scala.collection.concurrent.Map[String,Int] = Map()
    
    scala> mm.replace("five",5)
    res0: Option[Int] = None
    
    scala> mm.getClass
    res1: Class[_ <: scala.collection.concurrent.Map[String,Int]] = class scala.collection.convert.Wrappers$JConcurrentMapWrapper
    

提交回复
热议问题