在Scala中,如果我们有一个将String映射到Set [String]的MultiMap,例如:
val a = Map(
"Account1" -> Set("Cars", "Trucks"),
"Account2" -> Set("Trucks", "Boats")
)
反转/反转它的优雅方式最终是:
Map(
"Boats" -> Set("Account2"),
"Cars" -> Set("Account1"),
"Trucks" -> Set("Account1", "Account2")
)
答案 0 :(得分:0)
对不起长衬里,你可以分解它,这只是一个快速&肮脏的解决方案,但它做的工作
scala> a.toList.flatMap{ case (k,v:Set[_]) => v map (x => (x,k))}.groupBy(_._1).map{ case (k,v:List[(String,String)]) => (k,v.map(_._2).toSet)}
res45: scala.collection.immutable.Map[String,scala.collection.immutable.Set[String]] = Map(Boats -> Set(Account2), Trucks -> Set(Account1, Account2), Cars -> Set(Account1))
或者如果您更喜欢可读版本(与上面相同,只是细分为行):):
scala> val aTuples = a.toList.flatMap{ case (k,v:Set[_]) => v map (x => (x,k))}
aTuples: List[(String, String)] = List((Cars,Account1), (Trucks,Account1), (Trucks,Account2), (Boats,Account2))
scala> val grouped = aTuples.groupBy(_._1)
grouped: scala.collection.immutable.Map[String,List[(String, String)]] = Map(Boats -> List((Boats,Account2)), Trucks -> List((Trucks,Account1), (Trucks,Account2)), Cars -> List((Cars,Account1)))
scala> val flattenAndToSet = grouped.map{ case (k,v:List[(String,String)]) => (k,v.map(_._2).toSet)}
flattenAndToSet: scala.collection.immutable.Map[String,scala.collection.immutable.Set[String]] = Map(Boats -> Set(Account2), Trucks -> Set(Account1, Account2), Cars -> Set(Account1))