Scala Set apply()方法及示例
apply() 方法是用来测试集合中是否存在某些元素的。
方法定义:def apply(elem: A)
返回类型。如果某些元素存在于集合中,则返回true,否则返回false。
例子 #1:
// Scala program of apply()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating sets
val s1 = Set(1, 2, 3, 4, 5)
// Applying apply method
val result = s1.apply(3)
// Displays output
println(result)
}
}
输出。
true
例子#2。
// Scala program of apply()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating sets
val s1 = Set(1, 2, 3, 4, 5)
// Applying apply method
val result = s1.apply(10)
// Displays output
println(result)
}
}
输出。
false
极客教程