Scala Tutorial - Learn How To Use Scala's Immutable BitSet

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:40 pm

Overview

In this tutorial, we will learn how to use Scala's Immutable BitSet and perform common operations such as initialization, adding elements, adding BitSetsBitSet differences and intersection, and creating empty BitSet.

 

More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Immutable BitSet will be discussed in Chapter 8 on Collection Functions.

 

What is a BitSet?

Following on from the previous tutorials on SetHashSetTreeSet, and SortedSet, a BitSet also allows you to store unique elements with no repeatable values.

 

As per the Scala documentation, a BitSet represents a collection of small integers as the bits of a larger integer.

 

In this tutorial, we will focus on the set semantics of BitSet by re-using the BitSet example containing 3, 2, and 0 from the Scala documentation which represents the number 13 in decimal form.

 

Steps

1. How to initialize a BitSet with 3 elements

The code below shows how to initialize a BitSet with 3 elements.


import scala.collection.immutable.BitSet
println("Step 1: How to initialize a BitSet with 3 elements")
val bitSet1: BitSet = BitSet(3, 2, 0)
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 with 3 elements
Elements of bitSet1 = BitSet(0, 2, 3)

 

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 2 = ${bitSet1(2)}")
println(s"Element 3 = ${bitSet1(3)}")

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 2 = true
Element 3 = true

 

3. How to add elements in BitSet using +

The code below shows how to add elements in BitSet using +.


println("\nStep 3: How to add elements in BitSet using +")
val bitSet2: BitSet = bitSet1 + 13 + 13
println(s"Adding elements to BitSet using + = $bitSet2")
// NOTE: we only have one 13 element and not two as BitSet is distinct

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to add elements in BitSet using +
Adding elements to BitSet using + = BitSet(0, 2, 3, 13)

 

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 ++")
val bitSet3: BitSet = bitSet1 ++ BitSet(13, 14, 15, 16, 17)
println(s"Add two BitSets together using ++ = $bitSet3")

You should see the following output when you run your Scala application in IntelliJ:

Step 4: How to add two BitSets together using ++
Add two BitSets together using ++ = BitSet(0, 2, 3, 13, 14, 15, 16, 17)

 

5. How to remove element in BitSet using -

The code below shows how to remove element in BitSet using -.


println("\nStep 5: How to remove element in BitSet using -")
val bitSet4: BitSet = bitSet1 - 0
println(s"BitSet without element 0 = $bitSet4")

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to remove element in BitSet using -
BitSet without element 0 = BitSet(2, 3)

 

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 bitSet5: BitSet = BitSet(0, 2, 4)
println(s"Intersection of bitSet1 and bitSet5 = ${bitSet1 & bitSet5}")

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 bitSet5 = BitSet(0, 2)

 

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 bitSet5 = ${bitSet1 &~ bitSet5}")

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 bitSet5 = BitSet(3)

 

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()

 

This concludes our tutorial on Learn How To Use Scala's Immutable Bitset and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize a BitSet with 3 elements
  • How to check specific elements in BitSet
  • How to add elements in BitSet using +
  • How to add two BitSets together using ++
  • How to remove element in 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

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to use Scala's Immutable Stack.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on