×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet exists() 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 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.

Example 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]): Unit = {
        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.

Example 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]): Unit = {
        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.