I am just simulating an api with a fake database in memory and using scala.collection.mutable.HashMap[Int, AnyRef]
Which is the best collection to support concurrent inserts? There is a better choice?
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
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:The other way is to use concurrent mutable map, like
scala.collection.concurrent.TrieMap
, which doesn't require additional locking: