BitSet --() Method in Scala with Example

Here, we will learn about the BitSet --() method in Scala along with syntax and examples.
Submitted by Shivang Yadav, on October 12, 2020

BitSet in Scala is a special collection of positive integers. Scala programming language has a huge library containing a lot of utility functions to help working with data structure easy.

BitSet --() Method

The --() method for BitSet is used to create a new BitSet using two different BitSet. The resultant BitSet is the difference of the two BitSets.

The new BitSet will contain elements that are present in the first BitSet that are not present in the second BitSet.

Syntax:

BitSet1 -- BitSet2 

Return Type:

The function will return a new BitSet which is the difference between BitSet1 and BitSet2.

Program 1: Program to illustrate the working of -- method, when BitSet2 is a subset of BitSet1

// Scala program to illustrate the working of --() method 

import scala.collection.immutable.BitSet  
  
object MyObject{
    def main(args: Array[String]) {
        val BitSet1 = BitSet(3, 1, 8, 2, 6, 0, 9)
        val BitSet2 = BitSet(8, 2, 9)
        
        val NewBitSet  = BitSet1 -- BitSet2
        
        println("BitSet1 = " + BitSet1)
        println("BitSet2 = " + BitSet2)
        println("BitSet1 -- BitSet2 = " + NewBitSet)
    }
}

Output:

BitSet1 = BitSet(0, 1, 2, 3, 6, 8, 9)
BitSet2 = BitSet(2, 8, 9)
BitSet1 -- BitSet2 = BitSet(0, 1, 3, 6)

Program 2: Program to illustrate the working of -- method, when BitSet2 is equal to BitSet1

// Scala program to illustrate the working of --() method 

import scala.collection.immutable.BitSet  
  
object MyObject{
    def main(args: Array[String]) {
        val BitSet1 = BitSet(45, 12, 69, 45, 9, 2, 17)
        val BitSet2 = BitSet(45, 12, 69, 45, 9, 2, 17)
        
        val NewBitSet  = BitSet1 -- BitSet2
        
        println("BitSet1 = " + BitSet1)
        println("BitSet2 = " + BitSet2)
        println("BitSet1 -- BitSet2 = " + NewBitSet)
    }
}

Output:

BitSet1 = BitSet(2, 9, 12, 17, 45, 69)
BitSet2 = BitSet(2, 9, 12, 17, 45, 69)
BitSet1 -- BitSet2 = BitSet()

Program 3: Program to illustrate the working of -- method, when BitSet1 is subset of BitSet1

// Scala program to illustrate the working of --() method 

import scala.collection.immutable.BitSet  
  
object MyObject{
    def main(args: Array[String]) {
        val BitSet1 = BitSet(45, 12, 2, 17)
        val BitSet2 = BitSet(45, 12, 69, 45, 9, 2, 17)
        
        val NewBitSet  = BitSet1 -- BitSet2
        
        println("BitSet1 = " + BitSet1)
        println("BitSet2 = " + BitSet2)
        println("BitSet1 -- BitSet2 = " + NewBitSet)
    }
}

Output:

BitSet1 = BitSet(2, 12, 17, 45)
BitSet2 = BitSet(2, 9, 12, 17, 45, 69)
BitSet1 -- BitSet2 = BitSet()

From the above examples, we will understand that the if the first Bitset is equal or is subset of second BitSet, the method will create an empty Bitset.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.