×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet apply() Method in Scala

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

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

Load comments ↻





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