×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet exists() Method in Scala with Example

By IncludeHelp Last updated : November 09, 2024

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.

Example 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]): Unit = {
        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

Example 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]): Unit = {
        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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.