带有示例的 Scala TreeSet apply() 方法
在 Scala TreeSet class
中, apply()方法用于检查 TreeSet 中是否存在某些元素。
Method Definition: def apply(elem: A): Boolean
Return Type: It returns true if the given element is present in the TreeSet or else returns false.
示例 #1:
// Scala program of apply()
// method
// Import TreeSet
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(2, 1, 3, 4, 5)
// Print the TreeSet
println(t1)
// Applying apply() method
val result = t1.apply(3)
// Displays output
println("TreeSet contains '3': " + result)
}
}
输出:
TreeSet(1, 2, 3, 4, 5)
TreeSet contains '3': true
示例 #2:
// Scala program of apply()
// method
// Import TreeSet
import scala.collection.mutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating TreeSet
val t1 = TreeSet(2, 1, 3, 4, 5)
// Print the TreeSet
println(t1)
// Applying apply() method
val result = t1.apply(0)
// Displays output
println("TreeSet contains '0': " + result)
}
}
输出:
TreeSet(1, 2, 3, 4, 5)
TreeSet contains '0': false