Home »
Scala programs
BitSet exists() Method in Scala with Example
Here, we will learn about the BitSet exists() method in Scala along with syntax and examples.
Submitted by Shivang Yadav, on October 12, 2020
BitSet in Scala is a special collection of positive integers. Scala programming language has a huge library containing a lot of utility functions to help working with data structure easy.
BitSet exists() method
The exists() method in Scala is used to check whether an element exists in the collection (BitSet) that satisfies the given condition.
The method returns a boolean value based on the condition. Returns true if any element of the BitSet satisfies the condition otherwise returns false.
Syntax:
BitSetName.exists(x => {condition})
Return Type:
The method returns a Boolean value depending on the condition.
Program 1: Program to illustrate the working of exists() method
// 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")
}
}
Output:
Element satisfies the given condition
Program 2: Program to illustrate the working of exists() method
// 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")
}
}
Output:
No element satisfies the given condition