BitSet exists() Method in Scala with Example

Here, we will learn about BitSet equals() method in Scala. It is basically used to compare bitsets in Scala. We will learn its definition, syntax, and working examples.
Submitted by Shivang Yadav, on December 04, 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 equals() method

BitSet equals() method is used to compare bitsets with another bitset in Scala. It returns true and false based on the comparison between the elements of two bitsets.

Syntax:

bitset1.equals(bitset2)

Parameters:

The method accepts a bitset as a parameter which is compared with the calling bitset.

Return Type:

Returns a boolean value i.e. true or false based on the comparison of two bitsets.

Program 1: program to illustrate the working of our solution

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

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]) {
        val bitset1 = BitSet(4, 1, 3, 6, 2)
        val bitset2 = BitSet(2, 6, 3, 4, 1)
        
        println("bitset1 : " + bitset1)    
        println("bitset2 : " + bitset2)
        
        val isequal = bitset1.equals(bitset2)
        
        if(isequal)
            println("bitset1 is equals to bitset2")
        else
            println("bitset1 is not equals to bitset2")
    }
}

Output:

bitset1 : BitSet(1, 2, 3, 4, 6)
bitset2 : BitSet(1, 2, 3, 4, 6)
bitset1 is equals to bitset2

Explanation: In the above code, we have created two bitsets bitset1 and bitset2 with the same elements in a different order, and printed their values. On these bitsets, we will perform equals method and print the result.

Program 2: Program to illustrate the working of our solution

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

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]) {
        val bitset1 = BitSet(1, 3, 6, 2, 9)
        val bitset2 = BitSet(2, 1, 3, 5, 7)
        
        println("bitset1 : " + bitset1)    
        println("bitset2 : " + bitset2)
        
        val isequal = bitset1.equals(bitset2)
        
        if(isequal)
            println("bitset1 is equals to bitset2")
        else
            println("bitset1 is not equals to bitset2")
    }
}

Output:

bitset1 : BitSet(1, 2, 3, 6, 9)
bitset2 : BitSet(1, 2, 3, 5, 7)
bitset1 is not equals to bitset2



Comments and Discussions!

Load comments ↻






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