Scala Collections: Use the Set value to find the key from the Map object.

This is a scala question.

I currently have the following two collection objects:

val keywordLookup = Map("a" -> "1111",
                 "b" -> "2222",
                 "c" -> "3333",
                 "d" -> "4444",
                 "e" -> "5555")

val keywordList = Set("1111", "3333")

      

The Lookup keyword is a search object. The List keyword contains a list of values ​​that I need to find the ids from the keywordLookup object.

I would like to get the following result:

Map("a" -> "1111", "c" -> "3333")

      

+3


source to share


3 answers


val filtered = keywordLookup.filter(kv => keywordList.contains(kv._2))

      



filtered

is Map

which you want as output

+2


source


keywordLookup.filter(x => keywordList.contains(x._2))

      



+2


source


Using flatMap

on find

,

keywordList.flatMap (k => keywordLookup.find( _._2 == k)).toMap

      

0


source







All Articles