BitSet apply() Method in Scala with Example

Here, we will learn about apply() method defined on BitSet in Scala. It is used to find the presence of elements in BitSet. We will learn about apply() method in Scala with syntax and examples.
Submitted by Shivang Yadav, on December 10, 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 apply() method

BitSet apply() method is used to check whether the give element is present in the BitSet. It returns true or false based on the presence of element in BitSet.

Syntax:

BitSet_Name.apply(element)

Parameters:

The method accepts a single parameter which is the element to be check for.

Return Type:

It returns a boolean value based on the presence of element.

Let's take an example to understand the working of apply() method on BitSet.

Example 1: Program to illustrate the working of apply() method when element is present in BitSet

// Program to illustrate the working of apply() method

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]) {
        val MyBitSet = BitSet(4, 1, 6, 2, 9)
        println("The elemets of MyBitSet : " + MyBitSet)
        
        val element = 6
        val isPresent = MyBitSet.apply(element)
        
        if(isPresent)
            println("The element is present in the BitSet")
        else 
            println("The element is not present in the BitSet")
    }
}

Output:

The elemets of MyBitSet : BitSet(1, 2, 4, 6, 9)
The element is present in the BitSet

Explanation: In the above code, we have created a BitSet named MyBitSet and printed its value. And created an element. Tested its presence using apply method, and if its true print one statement otherwise print another statement.

Program 2: Program to illustrate the working of apply() method when element is not present in BitSet

// Program to illustrate the working of apply() method

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]) {
        val MyBitSet = BitSet(4, 1, 6, 2, 9)
        println("The elemets of MyBitSet : " + MyBitSet)
        
        val element = 7
        val isPresent = MyBitSet.apply(element)
        
        if(isPresent)
            println("The element is present in the BitSet")
        else 
            println("The element is not present in the BitSet")
    }
}

Output:

The elemets of MyBitSet : BitSet(1, 2, 4, 6, 9)
The element is not present in the BitSet

Explanation: In the above code, we have created a BitSet named MyBitSet and printed its value. And created an element. Tested its presence using apply method, and if its true print one statement otherwise print another statement.





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.