I have a map using the multimap trait, like so
val multiMap = new HashMap[Foo, Set[Bar]] with MultiMap[Foo, Bar]
I would like to combine filtering this map on specific values
multiMap.values.filter(bar => barCondition)
with flattening the matching results into a list of tuples of the form
val fooBarPairs: List[(Foo, Bar)]
What would be the idiomatic way of doing this? I was hoping that Scala might provide something like an anamorphism to do this without looping, but as a complete newbie I am not sure what my options are.
I have a map using the multimap trait, like so val multiMap = new
Share
Here’s an example:
I think the easiest way to get what you want is to use a for-expression:
You need the
.toSeqor the output type will be aMap, which would mean each mapping is overidden by subsequent elements. UsetoListon this output if you need aListspecifically.