Scala Map exists() 方法与示例
exists()方法用于检查给定谓词是否满足映射的元素。
Method Definition: def exists(p: ((A, B)) => Boolean): Boolean
Return Type: It returns true if the stated predicate holds true for some elements of the map else it returns false.
示例 #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