Scala TreeSet apply()方法及示例
在Scala TreeSet类中, apply() 方法被用来检查TreeSet中是否存在某些元素。
方法定义: def apply(elem: A):Boolean
返回类型。如果给定的元素存在于TreeSet中,则返回true,否则返回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
极客教程