I am trying to implement a nested map in scala in play framework.
what I am trying to achieve is a map which is of type Map[String, Map[Long,Int]]. I am not able to assign the value of the parent map which is itself a map. The application is compiling properly but the value is not getting updated. Can you please suggest what I am doing wrong and how to assign a map value to another map?
import java.util.concurrent.ConcurrentHashMap
import scala.collection._
import scala.collection.convert.decorateAsScala._
import scala.collection.JavaConversions.mapAsScalaMap
case class Events(eventType: String, timeStamp: Long)
object Events {
var eventMap = new ConcurrentHashMap[String, ConcurrentHashMap[Long,Int]]()
def save(events: Events) = {
var eventsKey: String = ""
var count: Int = 1
var timeKey: Long = 0L
var newEntry: Boolean = false
eventsKey = events.eventType
var countMap = new ConcurrentHashMap[Long,Int]()
countMap.clear()
if (eventMap.containsKey(eventsKey)) {
countMap = eventMap.get(eventsKey) // this is not working
timeKey = events.timeStamp
if (countMap.containsKey(timeKey)) {
count = countMap(timeKey) + 1
countMap.put(timeKey, count)
eventMap.put(eventsKey, countMap)
} // End of counter check
else {
newEntry = true
}
} // End of if event key check
else {
newEntry = true
} // End of else event key check
if (newEntry) {
countMap.putIfAbsent(timeKey, 1)
eventMap.putIfAbsent(eventsKey, countMap)
}
}
}
0L.save("aaaabc", 123323)