×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet diff() Method in Scala

By IncludeHelp Last updated : November 14, 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 diff() method

BitSet diff() method is used to find the difference between two bitSet's in Scala. For two BitSets, the method returns the elements that are in BitSet1 but not in BitSet2.

Syntax

BitSet1.diff(BitSet2)

Parameters

The method accepts a single parameter which is a BitSet whose elements are to be removed from the calling BitSet.

Return Type

The method returns A BitSet which consists of all elements remained after finding the difference.

Now let's see some examples of the working of diff() methods under different cases.

Example 1

Program to illustrate the working of diff() method

// Program to illustrate the working of diff() method

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]): Unit = {
        val BitSet1 = BitSet(5, 1, 7, 2, 0)
        val BitSet2 = BitSet(2, 0, 7)
        
        println("BitSet 1 : " + BitSet1)
        println("BitSet 2 : " + BitSet2)        
        
        val resultBitSet = BitSet1.diff(BitSet2)
        println("The resultant bitSet after finding difference is " + resultBitSet)
    }
}

Output

BitSet 1 : BitSet(0, 1, 2, 5, 7)
BitSet 2 : BitSet(0, 2, 7)
The resultant bitSet after finding difference is BitSet(1, 5)

Explanation

In the above code, we have created two BitSet's BitSet1 and BitSet2. And then using the diff() method, we have found the difference between BitSet1 and BitSet2, stored the result in resultBitSet, and printed it using println function.

Example 2

Program to illustrate the working of diff() method when both bitSets have no element in common

// Program to illustrate the working of diff() method

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]): Unit = {
        val BitSet1 = BitSet(5, 1, 7, 2, 0)
        val BitSet2 = BitSet(9, 3, 8)
        
        println("BitSet 1 : " + BitSet1)
        println("BitSet 2 : " + BitSet2)        
        
        val resultBitSet = BitSet1.diff(BitSet2)
        println("The resultant bitSet after finding difference is " + resultBitSet)
    }
}

Output

BitSet 1 : BitSet(0, 1, 2, 5, 7)
BitSet 2 : BitSet(3, 8, 9)
The resultant bitSet after finding difference is BitSet(0, 1, 2, 5, 7)

Explanation

In the above code, we have created two BitSet's BitSet1 and BitSet2 with different elements. Then we have used the diff() method to find the difference between the BitSet's and stored the result into a new BitSet named resultBitSet which is equal to BitSet1 as no element is deleted, and printed it.

Example 3

Program to illustrate the working of diff() function when BitSet1 ⊆ Bitset2

// Program to illustrate the working of diff() method

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]): Unit = {
        val BitSet1 = BitSet(5, 1, 7, 2, 0)
        val BitSet2 = BitSet(9, 5, 1, 7, 2, 0, 8)
        
        println("BitSet 1 : " + BitSet1)
        println("BitSet 2 : " + BitSet2)        
        
        val resultBitSet = BitSet1.diff(BitSet2)
        println("The resultant bitSet after finding difference is " + resultBitSet)
    }
}

Output

BitSet 1 : BitSet(0, 1, 2, 5, 7)
BitSet 2 : BitSet(0, 1, 2, 5, 7, 8, 9)
The resultant bitSet after finding difference is BitSet()

Explanation

In the above code, we have created two BitSet's BitSet1 and BitSet2 in such a way that BitSet1 is a subset of Bitset2. Then, we used the diff() method to find the difference between them which resulted in an empty BitSet. And then printed this BitSet.

Comments and Discussions!

Load comments ↻





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