📜  Scala Map exists() 方法与示例

📅  最后修改于: 2022-05-13 01:54:26.905000             🧑  作者: Mango

Scala Map exists() 方法与示例

exists()方法用于检查给定谓词是否满足映射的元素。

示例 #1:

// Scala program of exists()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating map
        val m1 = Map("geeks" -> 5, "for" -> 3)
          
        // Applying exists method
        val result = m1.exists(x => x._1 == "for" && x._2 == 3)
          
        // Displays output
        println(result)
      
    }
}
输出:
true

因此,此处所述谓词对于第二个键值对为 true,因此返回 true。
示例 #2:

// Scala program of exists()
// method
  
// Creating object
object GfG
{ 
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating map
        val m1 = Map("geeks" -> 5, "for" -> 3)
          
        // Applying exists method
        val result = m1.exists(x => x._1 == "geeks" && x._2 == 3)
          
        // Displays output
        println(result)
      
    }
}
输出:
false