Suppose I have a scala immutable Map[(String, Boolean), Set[String]]
, e.g.:
val myMap = Map(("a",true) -> Set("b","c"))
Now suppose I only know the first element of the key tuple, "a"
. Can (and how if yes) I get the map's value for this key if:
1) I know that the key could either be ("a", true)
or ("a", false)
(there cannot be two keys with the same String as the first element)
2) I don't know that, in which case I wish to get some kind of concatenation on the possible two values, such as v1.union(v2)
or v1 ++ v2
, etc.
Is there any scala "magic" I could use, like myMap.get(("a", Any))
?
In general, hash maps does not work like that, at least efficiently -- you still can scan map linearly and pick first key that matches criteria. In your particular case you can simply try to lookup record twice -- first with (key, true)
, then if nothing is found (key, false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With