What is the Scala Collection equivalent to Concurrenthashmap?

Asked

Viewed 411 times

5

Hello, how can I implement a Concurrenthashmap in Scala?

1 answer

6


In addition to the already constant in java, in the package java.util.concurrent, in scala we have a extension for this API, also to work with collections that are thread safe, the package scala.collection.concurrent. It has fewer classes/interfaces than java, until you can use the API classes in java.

Specifically in relation to ConcurrentHashMap java, which uses hash tables, there we have nothing equal, ie also implement map exclusively with hash tables.

What we have there is the TrieMap implementing Map, it combines in the implementation hash tables and associative vector of prefix trees.

If it’s not what you want to wear, you can wear something like this:

import scala.collection._
import scala.collection.convert.decorateAsScala._
import java.util.concurrent.ConcurrentHashMap

val cMap: concurrent.Map[String, String] = new ConcurrentHashMap().asScala

This way, use ConcurrentHashMap of java in scala like =)

P.S.: see also the commenting of @Anthonyaccioly, quite interesting the benchmark article.

  • 2

    Excellent answer. Follow the paper on Tries with some interesting benchmarks comparing the collection with ConcurrentHashMap, ConcurrentSkipListMap and NonBlockingHashTable: http://lampwww.epfl.ch/~prokopec/ctries-snapshot.pdf (of course a lot has changed in the implementation and in the JVM itself, but it’s interesting to see what kind of load each implementation shines in)

  • Thank you both this paper is excellent!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.