coffee
coffee

Reputation: 3088

Simple questions about collections in scala

I am just simulating an api with a fake database in memory and using scala.collection.mutable.HashMap[Int, AnyRef]

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

  2. Suppose I need another kind of collection like Map[Int, AnyRef], but this time the keys need to be sorted. A TreeMap is the best choice?

Thanks in advance

Upvotes: 1

Views: 324

Answers (3)

Dima
Dima

Reputation: 40510

I disagree with the above advise. If you are going to have mutable state anyway, you are better off isolating it inside the data container rather than replacing the container itself every time.

You are better off using java containers for this purpose. For a hashmap, java ConcurrentHashMap is your best choice. For a sorted implementation, you'll have to synchronize explicitly:

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

You can import scala.collection.JavaConversions._ to implicitly convert those into scala Maps, to get goodies, like map, filter etc., But ... you probably should not. Using any of those wouldn't be a good idea under concurrency in 99% of cases. Anything, other than you regular get and put (and, put/computeIfNotExists for the ConcurrentHashmap case) primitives would be non-trivial to implement and dangerous to use.

Think of these as just primitive key-value containers, not full-blown scala collections.

Upvotes: 0

adamwy
adamwy

Reputation: 1239

You have two options here.

You can use immutable data structures like scala.collection.immutable.HashMap, which provides highly efficient immutable hash map. You also need to remember that every update to this map needs to be synchronized like this:

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))
}

The other way is to use concurrent mutable map, like scala.collection.concurrent.TrieMap, which doesn't require additional locking:

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)
}

Upvotes: 1

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

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

Use immutable data structures

TreeMap is the best choice?

Yes.

Straight forward way to achieve thread safety is by using immutable data structures.

Scala provides immutable data structures. Just import scala.collection.immutable._.

In order to be sorted use scala.collection.immutable.TreeMap

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

Upvotes: 1

Related Questions