×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet in Scala

By IncludeHelp Last updated : November 09, 2024

Set is a collection of unique elements.

Scala BitSet

Bitset is a set of positive integers represented as a 64-bit word.

In the Scala programming language, BitSet can be mutable as well as immutable.

In mutable BitSet, bits can be changed in the program. Using scala.collection.mutable.BitSet

In immutable BitSet, bits cannot be changed in the program. Using scala.collection.immutable.BitSet

Creation of a new BitSet

To create a BitSet in Scala, import scala.collection.immutable.BitSet and initialize it with a set of integers representing bit positions, like BitSet(0, 1, 2, 3).

Following is the syntax to create a BitSet in Scala:

var bitset : Bitset = Bitset(elements...)

Example

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]): Unit = {
        val bitSet: BitSet = BitSet(0, 1, 2, 3) 
        println("Elements of new BitSet are " + bitSet) 
    }
}

Output

Elements of new BitSet are BitSet(0, 1, 2, 3)

Search for an element in BitSet

Search operation on BitSet in Scala is quite easy and passing the elements to be searched in BitSet, and it will return true or false based on the search.

Example

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]): Unit = {
        val bitSet: BitSet = BitSet(0, 13, 25, 39, 50) 
        println("Elements of new BitSet are " + bitSet) 
        println("Element 25 is in the BitSet? " + bitSet(25))
        println("Element 34 is in the BitSet? " + bitSet(34))
    }
}

Output

Elements of new BitSet are BitSet(0, 13, 25, 39, 50)
Element 25 is in the BitSet? true
Element 34 is in the BitSet? false

Adding elements to the BitSet

You can add one or multiple elements in a BitSet in Scala. The operators + and ++ are used to add single and multiple elements in BitSet in Scala. The operation will require new BitSet to hold the updated BitSet.

Example

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]): Unit = {
        val bitSet: BitSet = BitSet(0, 13, 25, 39, 50) 
        println("Elements of original BitSet are " + bitSet) 

        println("Adding a new element to BitSet: ")
        val bitSet2: BitSet = bitSet + 45 
        println("Elements of new BitSet are " + bitSet2) 

        println("Adding multiple new elements to BitSet: ")
        val bitSet3: BitSet = bitSet2 ++ BitSet(34, 54)
        println("Elements of new BitSet are " + bitSet3) 
    }
}

Output

Elements of new BitSet are BitSet(0, 13, 25, 39, 50)
Adding new elements to BitSet : 
Elements of new BitSet are BitSet(0, 13, 25, 39, 45, 50)
Adding new elements to BitSet : 
Elements of new BitSet are BitSet(0, 13, 25, 34, 39, 45, 50, 54)

Deleting Elements from BitSet

You can delete elements from BitSet in Scala. The operator - is used to delete an element from BitSet. The operations will require new BitSet to hold the updated BitSet.

Example

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]): Unit = {
        val bitSet: BitSet = BitSet(0, 13, 25, 39, 50) 
        println("Elements of original BitSet are " + bitSet) 
        
        println("Deleting element from BitSet:")
        val bitSet2: BitSet = bitSet - 25 
        println("Elements of new BitSet are " + bitSet2)
    }
}

Output

Elements of new BitSet are BitSet(0, 13, 25, 39, 50)
Deleting element from BitSet : 
Elements of new BitSet are BitSet(0, 13, 39, 50)

Creating Empty BitSet

An empty set can also be created in Scala. The empty keyword is used to create an empty BitSet in Scala.

Example

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]): Unit = {
        val bitSet: BitSet = BitSet.empty
        println("Elements of new BitSet are " + bitSet)
    }
}

Output

Elements of new BitSet are BitSet()

Comments and Discussions!

Load comments ↻





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