Scala Mutable SortedMap exists() 方法与示例
exists()方法用于检查给定谓词是否满足 SortedMap 的元素。
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 SortedMap else it returns false.
示例 #1:
// Scala program of exists()
// method
import scala.collection.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedMap
val m1 = SortedMap("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
import scala.collection.SortedMap
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating SortedMap
val m1 = SortedMap("geeks" -> 5, "for" -> 3)
// Applying exists method
val result = m1.exists(x => x._1 == "geeks" && x._2 == 3)
// Displays output
println(result)
}
}
输出:
false