BitSet DropWhile() Method in Scala with Example

Here, we will learn about BitSet dropWhile() method in Scala. It is used to drop the first element from the BitSet that satisfies the given condition. We will learn about dropWhile 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 dropWhile() method

BitSet dropWhile() method is used to delete the first element that satisfies the given condition and return the remaining BitSet.

Syntax:

bitset_name.dropWhile(condition)

Parameters:

It accepts a single parameter which is the condition to delete the element.

Return Type:

Returns a BitSet which contains elements after deleting.

Program 1: Example to delete element from BitSet using dropWhile() method

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

import scala.collection.immutable.BitSet

object MyObject {
    def main(args: Array[String]) {
        val myBitset = BitSet(8, 3, 6, 2, 9)
    
        println("myBitset : " + myBitset)    
    
        val newBitset = myBitset.dropWhile(x => {x % 2 == 0} )
    
        println("myBitset after deleting elements using dropWhile : " + newBitset)    
    }
}

Output:

myBitset : BitSet(2, 3, 6, 8, 9)
myBitset after deleting elements using dropWhile : BitSet(3, 6, 8, 9)

Explanation: In the above code, we have created a BitSet named myBitset in Scala. Then dropped the first element which is even, then print the resulting BitSet.



Comments and Discussions!

Load comments ↻





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