Scala Tutorial - Learn How To Use Scala's Mutable BitSet
Overview
In this tutorial, we will learn how to use Scala's Mutable BitSet to perform common operations such as initialize a BitSet, check specific elements, add and remove elements, BitSet intersection and difference, and create an empty BitSet.
And, don't forget to review the Data Structures tutorial before delving into Scala's Immutable and Mutable collections.
More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Mutable BitSet will be discussed in Chapter 8 on Collection Functions.
What is a BitSet?
As per Wikipedia, a Set is a data structure which allows you to store elements which are not repeatable. A Set also does not guarantee the ordering of elements.
As per the Scala Documentation, a BitSet represents a collection of small integers as the bits of a larger integer.
Steps
1. How to initialize a BitSet
The code below shows how to initialize a BitSet.
import scala.collection.mutable.BitSet
println("\nStep 1: How to initialize a BitSet")
val bitSet1: BitSet = BitSet(0, 2, 4, 6, 8)
println(s"Elements of bitSet1 = $bitSet1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a BitSet
Elements of bitSet1 = BitSet(0, 2, 4, 6, 8)
2. How to check specific elements in BitSet
The code below shows how to check specific elements in BitSet.
println("\nStep 2: How to check specific elements in BitSet")
println(s"Element 0 = ${bitSet1(0)}")
println(s"Element 1 = ${bitSet1(1)}")
println(s"Element 2 = ${bitSet1(2)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to check specific elements in BitSet
Element 0 = true
Element 1 = false
Element 2 = true
3. How to add elements to BitSet using +=
The code below shows how to add elements to BitSet using +=
println("\nStep 3: How to add elements to BitSet using +=")
bitSet1 += 10
println(s"Elements of bitSet1 after adding element 10 = $bitSet1")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to add elements to BitSet using +=
Elements of bitSet1 after adding element 10 = BitSet(0, 2, 4, 6, 8, 10)
4. How to add two BitSets together using ++=
The code below shows how to add two BitSets together using ++=.
println("\nStep 4: How to add two BitSets together using ++=")
bitSet1 ++= BitSet(12, 14, 16, 18, 20)
println(s"Elements of bitSet1 after adding second BitSet = $bitSet1")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to add two BitSets together using ++=
Elements of bitSet1 after adding second BitSet = BitSet(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
5. How to remove element from BitSet using -=
The code below shows how to remove element from BitSet using -=.
println("\nStep 5: How to remove element from BitSet using -=")
bitSet1 -= 0
println(s"bitSet1 without element 0 = $bitSet1")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to remove element from BitSet using -=
bitSet1 without element 0 = BitSet(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
6. How to find the intersection between two BitSets using &
The code below shows how to find the intersection between two BitSets using &.
println("\nStep 6: How to find the intersection between two BitSets using &")
val bitSetEven2: BitSet = BitSet(6, 8, 10)
println(s"Intersection of bitSet1 and bitSetEven2 = ${bitSet1 & bitSetEven2}")
You should see the following output when you run your Scala application in IntelliJ:
Step 6: How to find the intersection between two BitSets using &
Intersection of bitSet1 and bitSetEven2 = BitSet(6, 8, 10)
7. How to find the difference between two BitSets using &~
The code below shows how to find the difference between two BitSets using &~.
println("\nStep 7: How to find the difference between two BitSets using &~")
println(s"Difference of bitSet1 and bitSetEven2 = ${bitSet1 &~ bitSetEven2}")
You should see the following output when you run your Scala application in IntelliJ:
Step 7: How to find the difference between two BitSets using &~
Difference of bitSet1 and bitSetEven2 = BitSet(2, 4, 12, 14, 16, 18, 20)
8. How to initialize an empty BitSet
The code below shows how to initialize an empty BitSet.
println("\nStep 8: How to initialize an empty BitSet")
val emptyBitSet: BitSet = BitSet.empty
println(s"Empty BitSet = $emptyBitSet")
You should see the following output when you run your Scala application in IntelliJ:
Step 8: How to initialize an empty BitSet
Empty BitSet = BitSet()
Summary
In this tutorial, we went over the following:
- How to initialize a BitSet
- How to check specific elements in BitSet
- How to add elements to BitSet using +=
- How to add two BitSets together using ++=
- How to remove element from BitSet using -=
- How to find the intersection between two BitSets using &
- How to find the difference between two BitSets using &~
- How to initialize an empty BitSet
Tip
- Don't forget to review Chapter 8 on Collection Functions where we will go over the rich suite of functions exposed on collections such as aggregate, collect, fold, reduce, map, flatMap, scan, partition etc
- Scala's documentation on mutable BitSet.
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
If you have followed the previous tutorials, you should by now feel more comfortable with using the Mutable Collection in Scala.
This would be a good place to proceed to Chapter 8 where I will go over the Collection Functions such as aggregate, fold, reduce, map, flatMap etc.