Skip to main content
The 2024 Developer Survey results are live! See the results
added 53 characters in body
Source Link
ghik
  • 10.8k
  • 1
  • 38
  • 50

Use Guava's MapMaker.

An example from the doc shows exactly what you need (translated into Scala):

ConcurrentMap<Request,import Stopwatch>java.util.concurrent.ConcurrentMap

val timers: =ConcurrentMap[Request, newStopwatch] MapMaker()= 
    new MapMaker().concurrencyLevel(4)
    .weakKeys()
    .makeMap();

This is, of course, Java map, but you can easily wrap this mapit into Scala map:

import java.{util => ju}
import scala.collection.JavaConverters._
import scala.collection.mutable

val javaMap: ju.Map[String,String] = ...
val scalaMap: mutable.Map[String,String] = javaMap.asScala

Use Guava's MapMaker.

An example from the doc shows exactly what you need:

ConcurrentMap<Request, Stopwatch> timers = new MapMaker()
    .concurrencyLevel(4)
    .weakKeys()
    .makeMap();

This is, of course, Java, but you can easily wrap this map into Scala map:

import java.{util => ju}
import scala.collection.JavaConverters._
import scala.collection.mutable

val javaMap: ju.Map[String,String] = ...
val scalaMap: mutable.Map[String,String] = javaMap.asScala

Use Guava's MapMaker.

An example from the doc shows exactly what you need (translated into Scala):

import java.util.concurrent.ConcurrentMap

val timers: ConcurrentMap[Request, Stopwatch] = 
    new MapMaker().concurrencyLevel(4).weakKeys.makeMap

This is, of course, Java map, but you can easily wrap it into Scala map:

import java.{util => ju}
import scala.collection.JavaConverters._
import scala.collection.mutable

val javaMap: ju.Map[String,String] = ...
val scalaMap: mutable.Map[String,String] = javaMap.asScala
Source Link
ghik
  • 10.8k
  • 1
  • 38
  • 50

Use Guava's MapMaker.

An example from the doc shows exactly what you need:

ConcurrentMap<Request, Stopwatch> timers = new MapMaker()
    .concurrencyLevel(4)
    .weakKeys()
    .makeMap();

This is, of course, Java, but you can easily wrap this map into Scala map:

import java.{util => ju}
import scala.collection.JavaConverters._
import scala.collection.mutable

val javaMap: ju.Map[String,String] = ...
val scalaMap: mutable.Map[String,String] = javaMap.asScala