Set drop() Method in Scala

Scala | Set drop() Method: In this article, we will learn about drop() method of set collection in Scala. It is basically used to drop items from the set. Here, we will learn its syntax and working examples.
Submitted by Shivang Yadav, on November 24, 2020

The drop() method defined for the set collection in Scala is used to drop (delete) elements from the set. The drop() method deletes the first n elements from the given set and returns the rest.

Syntax:

drop(n): set

Parameters:

  • n - The method accepts a single parameter n, which is the count of numbers to be deleted from the set.

Return value:

The method returns a set that contains elements that remained in the set after dropping the first n elements.

Points to Remember:

  1. Drop method deletes first n elements of the set.
  2. The parameter can have any positive value.
  3. If the number of elements to be deleted is equal to or greater than the size of the set. An empty set is returned.

Examples:

Example to illustrate the working of the drop method.

Example 1: Deleting first n elements from a set of size l, l > n.

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

object MyClass {
    def main(args: Array[String]) {
        val mySet = Set(5, 1, 9, 4, 0, 7, 2)
        println("The initial set 'mySet' : " + mySet)
        
        // Deleting first 4 elemets from the 
        // set using drop method 
        val newSet = mySet.drop(4);
        println("The set after deleting '4' elements using drop method: " + newSet)
    }
}

Output:

The initial set 'mySet' : HashSet(0, 5, 1, 9, 2, 7, 4)
The set after deleting '4' elements using drop method: HashSet(2, 7, 4)

Explanation:

In the above code, we have created a set mySet with 7 elements and printed it using println method. After this, we have used to drop() method to delete the first 4 elements of the set and store the result in a new set newSet and then printed the result.

Code 2: A different way to print the values of the list.

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

object MyClass {
    def main(args: Array[String]) {
        val mySet = Set(5, 1, 9, 4, 0, 7, 2)
        
        print("The initial set 'mySet' : ")
        for(x <- mySet)
            print(x + " ")
        
        // Deleting first 4 elemets from the 
        // set using drop method 
        val newSet = mySet.drop(4);
        print("\nThe set after deleting '4' elements using drop method : ")
        for(x <- newSet)
            print(x + " ")
    }
}

Output:

The initial set 'mySet' : 0 5 1 9 2 7 4 
The set after deleting '4' elements using drop method : 2 7 4 

Example 2: Deleting first n elements from the list of size l, where l > n.

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

object MyClass {
    def main(args: Array[String]) {
        val mySet = Set(5, 1, 9, 4, 0, 7, 2)
        print("The initial set 'mySet' : " + mySet)
        
        // Deleting first 4 elemets from the 
        // set using drop method 
        val newSet = mySet.drop(10);
        println("\nThe set after deleting '10' elements using drop method : " + newSet)
    }
}

Output:

The initial set 'mySet' : HashSet(0, 5, 1, 9, 2, 7, 4)
The set after deleting '10' elements using drop method : HashSet()

Explanation:

In this above code, we created a set named mySet with elements 7 and printed it. Then we have deleted 10 values from it. The method does not throws an error, instead return an empty set.




Comments and Discussions!

Load comments ↻






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