BitSet drop() Method in Scala with Example

Here, we will learn about BitSet drop() method in Scala. It is used to drop elements from the BitSet. We will learn about the drop method with syntax and 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 drop() method

BitSet drop() method is used to delete first n elements from the BitSet.

Syntax:

bitset_name.drop(n)

Parameters:

It accepts a single parameter (n) which is the number of elements to be dropped.

Return Type:

Returns a BitSet which contains the elements that remained after dropping.

Program 1: Program to illustrate the working of drop method in Scala

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

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]) {
        val myBitset = BitSet(1, 3, 6, 2, 9)
        
        println("myBitset : " + myBitset)    
    
        val newBitset = myBitset.drop(3)
        
        println("myBitset after deleting 3 elements : " + newBitset)    
    }
}

Output:

myBitset : BitSet(1, 2, 3, 6, 9)
myBitset after deleting 3 elements : BitSet(6, 9)

Explanation: In the above code, we have created a BitSet named myBitset in Scala. Then dropped 3 elements using the drop method and printed the resulting BitSet.

Program 2: Program to illustrate the working of drop method in Scala

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

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]) {
        val myBitset = BitSet(1, 3, 6, 2, 9)
        
        println("myBitset : " + myBitset)    
        
        val newBitset = myBitset.drop(6)
        
        println("myBitset after deleting 6 elements : " + newBitset)    
    }
}

Output:

myBitset : BitSet(1, 2, 3, 6, 9)
myBitset after deleting 6 elements : BitSet()

Explanation: In the above code, we have created a BitSet named myBitset in Scala. Then dropped 6 elements using the drop method which is greater than the size of the array. Here, the method returns an empty BitSet. At last, we will print the resulting BitSet.




Comments and Discussions!

Load comments ↻






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