关于scala中集合的简单问题

Simple questions about collections in scala

我只是用内存中的假数据库模拟一个 api 并使用 scala.collection.mutable.HashMap[Int, AnyRef]

  1. 支持并发插入的最佳集合是哪个?还有更好的选择吗?

  2. 假设我需要另一种集合,如Map[Int, AnyRef],但这次需要对键进行排序。 TreeMap 是最好的选择?

提前致谢

Which is the best collection to support concurrent inserts? There is a better choice?

使用不可变数据结构

TreeMap is the best choice?

是的。

实现线程安全的直接方法是使用不可变数据 结构。

Scala 提供不可变的数据结构。只需导入 scala.collection.immutable._.

为了排序使用scala.collection.immutable.TreeMap

This post tells about how to use TreeMap and provide custom ordering

这里有两个选择。

您可以使用像 scala.collection.immutable.HashMap 这样的不可变数据结构,它提供了高效的不可变哈希映射。您还需要记住,此地图的每次更新都需要像这样同步:

object Database {
  private var map = new immutable.HashMap[Int, AnyRef]
  def get(index: Int) = map(index)
  def insert(index: Int, value: AnyRef) = 
    synchronized(map = map.updated(index, value))
}

另一种方法是使用并发可变映射,如 scala.collection.concurrent.TrieMap,它不需要额外的锁定:

object Database {
  private val map = new concurrent.TrieMap[Int, AnyRef]
  def get(index: Int) = map(index)
  def insert(index: Int, value: AnyRef) = map.put(index, value)
}

我不同意以上建议。如果你无论如何都要有可变状态,你最好将它隔离在数据容器中,而不是每次都替换容器本​​身。

您最好为此目的使用 java 容器。对于哈希图,java ConcurrentHashMap 是您的最佳选择。对于排序的实现,您必须显式同步:

 object DB {
   import java.util._
   val hashed = new concurrent.ConcurrentHashMap[String, AnyRef]
   val sorted = Collections.synchronizedMap(new TreeMap[Int, AnyRef])
}

您可以 import scala.collection.JavaConversions._ 将它们隐式转换为 scala 映射,以获得好东西,例如 mapfilter 等,但是... 您可能不应该。在 99% 的情况下,在并发情况下使用其中任何一个都不是一个好主意。除了常规 getput(以及 put/computeIfNotExists 用于 ConcurrentHashmap 情况)原语之外,任何原语都难以实现并且使用起来很危险。

将这些视为原始的键值容器,而不是成熟的 Scala 集合。