BitSet in Scala

A set is a collection which only contains unique items which are not repeatable. A BitSet is a collection of small integers as the bits of a larger integer. Non negative integers sets which represented as array of variable-size of bits packed into 64-bit words is called BitSets. The largest number stored in bitset is the memory of a bitset. It extends Set trait.

Syntax:

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

Where BS is the name of created BitSet

In Scala, BitSet have two versions: scala.collection.immutable.BitSet and scala.collection.mutable.BitSet. They are almost identical but the mutable version changes the bits in place so immutable data structures are much better for concurrency.

Operations perform with BitSet

Initialize a BitSet : Below is the example to create or initialize BitSet.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to initialize a BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        println("Initialize a BitSet")
          
        // Creating HashSet
        val bitSet: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bitSet")
    }

chevron_right


Output:

Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)

 
Check specific elements in BitSet :

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Check specific elements in BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        println("Initialize a BitSet")
          
        // Creating BitSet
        val bitSet: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bitSet")
          
        // Checking
        println(s"Element 2 = ${bitSet(2)}")
        println(s"Element 4 = ${bitSet(4)}")
    }

chevron_right


Output:

Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)
Element 2 = true
Element 4 = false

 
Adding an elements in BitSet : We can add an element in BitSet by using + sign. below is the example of adding an element in BitSet.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of adding an element in BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        println("Initialize a BitSet")
          
        // Creating BitSet
        val bs: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bs")
          
        // Adding an element in BitSet
        val bs1: BitSet = bs + 10 + 11
        println(s"Adding elements to BitSet = $bs1")
    }
}

chevron_right


Output:

Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)
Adding elements to BitSet = BitSet(0, 1, 2, 3, 10, 11)

 
Adding more than one element in BitSet : We can add more than one element in BitSet by using ++ sign. below is the example of adding more than one elements in BitSet.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of adding more elements in BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        println("Initialize a BitSet")
          
        // Creating BitSet
        val bs: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bs")
          
        // Adding elements in BitSet
        val bs1: BitSet = bs ++ BitSet(4, 5, 6)
        println(s"Add more than one elements to BitSet = $bs1")
    }
}

chevron_right


Output:

Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)
Add more than one elements to BitSet  = BitSet(0, 1, 2, 3, 4, 5, 6)

 
Remove element in BitSet : We can remove an element in BitSet by using – sign. below is the example of removing an element in BitSet.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of removing element in BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        println("Initialize a BitSet")
          
        // Creating BitSet
        val bs: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bs")
          
        // removing elements in BitSet
        val bs1: BitSet = bs - 2
        println(s"remove element from bitset = $bs1")
    }
}

chevron_right


Output:

Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)
remove element from bitset = BitSet(0, 1, 3)

 
Find the intersection between two BitSets : We can find intersection between two BitSets by using & sign. below is the example of finding intersection between two BitSets.

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of finding the intersection between two BitSets
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        println("Initialize two BitSets")
          
        // Creating two BitSet
        val bs: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements of bitset1 are = $bs")
          
        val bs1: BitSet = BitSet(4, 5, 3, 6)
        println(s"Elements of bitset2 are = $bs1")
          
        // finding the intersection between two BitSets
        println(s"Intersection of bitSet1 and bitSet2 = ${bs & bs1}")
    }
}

chevron_right


Output:

Initialize two BitSets
Elements of bitset1 are = BitSet(0, 1, 2, 3)
Elements of bitset2 are = BitSet(3, 4, 5, 6)
Intersection of bitSet1 and bitSet2 = BitSet(3)

 
Initializing an empty BitSet :

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of Initializing an empty BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
    // Main method
    def main(args:Array[String])
    
        // Initializing an empty BitSet
        val emptyBitSet: BitSet = BitSet.empty
        println(s"Empty BitSet = $emptyBitSet")
    }
}

chevron_right


Output:

Empty BitSet = BitSet()


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.