×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

BitSet drop() Method in Scala

By IncludeHelp Last updated : November 14, 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 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.

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

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