【问题标题】:Scala comparing two Map's keys and valuesScala 比较两个 Map 的键和值
【发布时间】:2016-03-08 21:42:43
【问题描述】:

上下文是在 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)他们有相同的地图键和 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,因为属性的键不是一对一的。

我认为必须有一种功能性的方法来做到这一点。

【问题讨论】:

  • 出于兴趣,两个案例类看起来一样,为什么两个案例类而不是一个?

标签: scala


【解决方案1】:

可能是这样的?

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                                                                                                  

【讨论】:

    猜你喜欢
    • 2019-04-20
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多