Home » Scala language

BitSet in Scala

In this tutorial, we will learn about the bitSet in Scala programming language with syntax and examples for creating bitset, searching for an element, adding elements to bitset, etc.
Submitted by Shivang Yadav, on December 24, 2019

Scala BitSet

Set is a collection of unique elements.

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

Syntax:

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

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

Example 1: Creation of a new BitSet

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]) {
        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)

Example 2: 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.

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]) {
        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

Example 3: 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.

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]) {
        val bitSet: BitSet = BitSet(0, 13, 25, 39, 50) 
        println("Elements of new BitSet are " + bitSet) 
        println("Adding new elements to BitSet : ")
        val bitSet2 : BitSet = bitSet + 45 
        println("Elements of new BitSet are " + bitSet2) 
        println("Adding 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)

Example 4: Deleting Elements from BitSet in Scala

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.

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]) {
        val bitSet: BitSet = BitSet(0, 13, 25, 39, 50) 
        println("Elements of new 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)

Example 5: Creating Empty BitSet in Scala

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

import scala.collection.immutable.BitSet

object MyClass {
    def main(args: Array[String]) {
        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.