Scala中的BitSet exist()方法与示例
Scala中的BitSet是正整数的特殊集合。Scala编程语言具有一个庞大的库,其中包含许多实用程序功能,以帮助轻松处理数据结构。
BitSetexists()方法
Scala中的exist()方法用于检查集合(BitSet)中是否存在满足给定条件的元素。
该方法根据条件返回布尔值。如果BitSet的任何元素满足条件,则返回true,否则返回false。
语法:
BitSetName.exists(x => {condition})返回类型:
该方法根据条件返回布尔值。
程序1:说明exists()方法工作的程序
// Scala program to illustrate the working of exists() method
import scala.collection.immutable.BitSet
object MyObject{
def main(args: Array[String]) {
val Bitset = BitSet(45, 12, 69, 45, 9, 2, 17)
if(Bitset.exists(x => {x % 5 == 0}))
println("Element satisfies the given condition")
else
println("No element satisfies the given condition")
}
}输出:
Element satisfies the given condition
程序2:说明exists()方法工作的程序
// Scala program to illustrate the working of exists() method
import scala.collection.immutable.BitSet
object MyObject{
def main(args: Array[String]) {
val Bitset = BitSet(12, 69, 9, 2, 17)
if(Bitset.exists(x => {x % 5 == 0}))
println("Element satisfies the given condition")
else
println("No element satisfies the given condition")
}
}输出:
No element satisfies the given condition