I have a certain map built from JSON and it has some hierarchy. For example:
"2015-05": {
"129557": {
"123456": 3,
"654321": 2,
"143526": 1
}
}
This is stored as a nested map. I wanted to have a simple method to access a key. I could do multiple checks at each key and then see if its present or not and then do it for the second key and so on. However this seems cumbersome. I instead chose to do something like this:
def getNumFromMap(key1: String, key2: Int, key3: String): Option[Int] ={
try{
map(key1)(key2).get(key3)
}catch{
case e: Exception => None
}
}
This function could be potentially executed millions of times. Does using a try/catch slow down the execution? Is there a better method to accomplish the same which is faster?