0 votes
in Scala Constructs by
Explain BitSet in scala

1 Answer

0 votes
by

A set is a collection of unique items that cannot be repeated. In Scala, non-negative integer sets are called Bitsets, and they are represented as variable-size arrays of bits packed into 64-bit words.   The largest number in a bitset represents its memory footprint.

Syntax: 

 var BS: BitSet = BitSet(element1, element2, element3, ....)

Here, BS represents the name of the BitSet that was created. 

scala.collection.immutable.BitSet and scala.collection.mutable.BitSet are the two versions of BitSet provided in Scala. Although both are identical, mutable data structures change the bits in place, thus making them less concurrency friendly than immutable data structures. 

Example:  

import scala.collection.immutable.BitSet 

object Madanswer 

{  

     def main(args:Array[String]) 

    {  

        println("Initialize a BitSet") 

        val numbers: BitSet = BitSet(5, 6, 7, 8) 

        println(s"Elements of BitSet are = $bitSet") 

        println(s"Element 6 = ${bitSet(6)}") 

        println(s"Element 3 = ${bitSet(3)}") 

        println(s"Element 7 = ${bitSet(7)}") 

    } 

Output: 

Initialize a BitSet 

Elements of BitSet are = BitSet(5,6,7,8) 

Element 6 = true 

Element 3 = false 

Element 7 = true

...