×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet --() Method in Scala

By IncludeHelp Last updated : November 09, 2024

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.

Example 1

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

import scala.collection.immutable.BitSet  

object MyObject {
    def main(args: Array[String]): Unit = {
        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)

Example 2

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

import scala.collection.immutable.BitSet  
  
object MyObject {
    def main(args: Array[String]): Unit = {
        val BitSet1 = BitSet(45, 12, 69, 9, 2, 17)
        val BitSet2 = BitSet(45, 12, 69, 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()

Example 3

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

import scala.collection.immutable.BitSet  
  
object MyObject {
    def main(args: Array[String]): Unit = {
        val BitSet1 = BitSet(45, 12, 2, 17)
        val BitSet2 = BitSet(45, 12, 69, 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.