Google News
logo
Scala - Interview Questions
What is a BitSet?
A bitset is a set of non-negative integers depicted as arrays. These arrays are variable in size and packed into 64-bit words. The largest number in a bitset determines its memory footprint. Let’s take an example.
scala> import scala.collection.immutable._
import scala.collection.immutable._
scala> var nums=BitSet(7,2,4,3,1)
nums: scala.collection.immutable.BitSet = BitSet(1, 2, 3, 4, 7)
scala> nums+=9  //Adding an element
scala> nums​

res : scala.collection.immutable.BitSet = BitSet(1, 2, 3, 4, 7, 9)

scala> nums-=4  //Deleting an element
scala> nums​

res : scala.collection.immutable.BitSet = BitSet(1, 2, 3, 7, 9)

scala> nums-=0  //Deleting an element that doesn’t exist
scala> nums​

res : scala.collection.immutable.BitSet = BitSet(1, 2, 3, 7, 9)
Advertisement