Reputation: 193
I've this code :
val total = ListMap[String,HashMap[Int,_]]
val hm1 = new HashMap[Int,String]
val hm2 = new HashMap[Int,Int]
...
//insert values in hm1 and in hm2
...
total += "key1" -> hm1
total += "key2" -> hm2
....
val get = HashMap[Int,String] = total.get("key1") match {
case a : HashMap[Int,String] => a
}
This work, but I would know if exists a better (more readable) way to do this. Thanks to all !
Upvotes: 3
Views: 4887
Reputation: 25834
First of all: this is a non-answer (as I would not recommend the approach I discuss), but it was too long for a comment.
If you haven't got too many different keys in your ListMap, I would suggest trying Malvolio's answer. Otherwise, due to type erasure, the other approaches based on pattern matching are practically equivalent to this (which works, but is very unsafe):
val get = total("key1").asInstanceOf[HashMap[Int, String]]
the reasons why this is unsafe (unless you like living dangerously) are:
Upvotes: 0
Reputation: 29528
Your total
map, containing maps with non uniform value types, would be best avoided. The question is, when you retrieve the map at "key1"
, and then cast it to a map of strings, why did you choose String
?
The most trivial reason might be that key1
and so on are simply constants, that you know all of them when you write your code. In that case, you probably should have a val
for each of your maps, and dispense with map of maps entirely.
It might be that the calls made by the client code have this knowledge. Say that the client does stringMap("key1"), or intMap("key2") or that one way or another, the call implies that some given type is expected. That the client is responsible for not mixing types and names. Again in that case, there is no reason for total. You would have a map of string maps, a map of int maps (provided that you are previous knowledge of a limited number of value types)
What is your reason to have total?
Upvotes: 0
Reputation: 297155
(edit: oh, sorry, I get it now)
Here's the thing: the code above doesn't work. Type parameters are erased, so the match above will ALWAYS return true -- try it with key2
, for example.
If you want to store multiple types on a Map
and retrieve them latter, you'll need to use Manifest
and specialized get
and put
methods. But this has already been answers on Stack Overflow, so I won't repeat myself here.
Upvotes: 0
Reputation: 44376
It looks like you're trying to re-implement tuples as maps.
val total : ( Map[Int,String], Map[Int,Int]) = ...
def get : Map[Int,String] = total._1
Upvotes: 4