What is the Scala Collection equivalent to ConcurrentHashMap?

5

Hello, how can I implement a ConcurrentHashMap in Scala?

    
asked by anonymous 02.06.2015 / 22:05

1 answer

6

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

Specifically regarding ConcurrentHashMap for java , which uses hash tables, there we have nothing equal, that is, that implements map exclusively with hash tables .

What we have is TrieMap that implements < the Map , it combines in the implementation vector associative de prefix trees .

If it's not what you want to use, you can use 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

In this way, use ConcurrentHashMap for java in scala like =)

PS: Also see the comment @AnthonyAccioly >, quite interesting the benchmark of the article.

    
02.06.2015 / 22:53