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 30, 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 in Scala is used to add two collections in scala. The addition is saved to a new BitSet.

One of the two collections to be added must be a BitSet otherwise the method will return a type mismatch error.

Syntax:

BitSet = BitSet_Name ++ collection_Name

Return type:

The method returns a new BitSet with all elements from the two collections passed to the method.

Program 1: Program to illustrate the working of ++ method in Scala by adding two BitSets in Scala.

// Scala program to illustrate the working of ++ method

import scala.collection.immutable.BitSet

object MyObject{
    def main(args: Array[String]) {
        val BitSet1 = BitSet(4, 1 ,7, 9, 3)
        val BitSet2 = BitSet(18, 100, 91, 66)
        
        println("BitSet1 : " + BitSet1)
        println("BitSet2 : " + BitSet2)
        
        // Adding new elements using + method     
        val newBitSet = BitSet1 ++ BitSet2
        println("Addition BitSet : " + newBitSet)
    }
}

Output:

BitSet1 : BitSet(1, 3, 4, 7, 9)
BitSet2 : BitSet(18, 66, 91, 100)
Addition BitSet : BitSet(1, 3, 4, 7, 9, 18, 66, 91, 100)

Program 2: Program to illustrate the working of ++ method in Scala by adding one bitSet and one collection.

// Scala program to illustrate the working of ++ method

import scala.collection.immutable.BitSet

object MyObject{
    def main(args: Array[String]) {
        val BitSet1 = BitSet(4, 1 ,7, 9, 3)
        val collection1 = Vector(18, 100, 91, 66)
    
        println("BitSet1 : " + BitSet1)
        println("collection1 : " + collection1)
        
        // Adding new elements using + method     
        val newBitSet: BitSet = BitSet1 ++ collection1
        println("Addition BitSet : " + newBitSet)
    }
}

Output:

BitSet1 : BitSet(1, 3, 4, 7, 9)
collection1 : Vector(18, 100, 91, 66)
Addition BitSet : BitSet(1, 3, 4, 7, 9, 18, 66, 91, 100)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.