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
阅读更多:Scala 教程
极客教程