Scala 设置 apply() 方法与示例
apply()方法用于测试集合中是否存在某些元素。
Method Definition: def apply(elem: A)
Return Type: It returns true if some elements are present in the set else it returns 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