Scala比较两个Map的键和值

时间:2016-03-08 21:42:43

标签: scala

上下文是在Scala中比较两个Map的键和值,但我不确定我是否知道如何正确地问这个。我们说我有两个案例类

case class WhiteList(attributes: Option[Map[String, Set[String]]])

case class Query(attributes: Option[Map[String, Set[String]]])

如果属性映射中的键相同,则我想比较这两个类之间的属性值,否则返回false

所以,如果我有

val wl = WhiteList(attributes = Some(Map("patterns" -> Set("plaid"),
                                         "colors" -> Set("blue", "orange")))

val q = Query(attributes = Some(Map("patterns" -> Set("plaid"), 
                                     "colors" -> Set("orange")))

如果我比较这两个,我想返回true,因为: 1)他们有相同的Map键和 2)相应键的值相交

如果我有

val q2 = Query(attributes = Some(Map("patterns" -> Set("stripes"), 
                                    "colors" -> Set("orange", "red", "blue")))

并将相应键的值与wl进行比较,我想要false,因为"模式"价值不相交。

如果我有

val q3 = Query(attributes = Some(Map("starwars" -> Set("a new hope", "empire strikes back"), 
                                     "patterns" -> Set("stripes"), 
                                     "colors" -> Set("orange", "red", "blue")))

并将q3与wl进行比较,我期待false,因为属性的键不是一对一的。

我认为必须有一种功能性的方法来实现这一目标。

1 个答案:

答案 0 :(得分:3)

也许是这样的?

def areEqual(wl: WhiteList, q: Query) = (wl, q) match {
  case (WhiteList(Some(map1)), Query(Some(map2))) =>
    map1.keySet == map2.keySet &&
      map1.keySet.forall(key => (map1(key) intersect map2(key)).nonEmpty) 
  case _ => false
}

在repl中测试:

val wl = WhiteList(attributes = Some(Map("patterns" -> Set("plaid"),                                                   
                                         "colors" -> Set("blue", "orange"))))                                          

val q = Query(attributes = Some(Map("patterns" -> Set("plaid"),                                                        
                                     "colors" -> Set("orange"))))                                                      

val q2 = Query(attributes = Some(Map("patterns" -> Set("stripes"),                                                     
                                    "colors" -> Set("orange", "red", "blue"))))                                        

val q3 = Query(attributes = Some(Map("starwars" -> Set("a new hope", "empire strikes back"),                           
                                     "patterns" -> Set("stripes"),                                                     
                                     "colors" -> Set("orange", "red", "blue"))))                                       

scala> areEqual(wl, q)                                                                                                 
res4: Boolean = true                                                                                                   

scala> areEqual(wl, q2)                                                                                                
res5: Boolean = false                                                                                                  

scala> areEqual(wl, q3)                                                                                                
res6: Boolean = false