dictionary - Scala: How to return the key that contains an item in their value where value is a Set? -


i have map[int, set[int]]and given item want return key indexes set item exists in. example if have map(1 -> set("a"), 2 -> set("b"), 3 -> set("c","z")). item "z" want return 3 because 3 key indexes set contains 3. here have, can't seem find way key. can value, set[int]. assume item in 1 possible set.

def find(djs: map[int, set[int]], item: int) :int = { ((key, set) <- djs) {   if (set.contains(item)) {     key   }  } 

collectfirst made this:

val m = map(1 -> set("a"), 2 -> set("b"), 3 -> set("c","z"))  m.collectfirst{case (k,v) if v.contains("z") => k} //> res0: option[int] = some(3) 

and forget set can used function (i.e. apply same contains)

m.collectfirst{case (k,v) if v("z") => k} //> res0: option[int] = some(3) 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -