Home »
Scala
Set drop() Method in Scala
By IncludeHelp Last updated : November 14, 2024
Set drop() Method
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
- Drop method deletes first n elements of the set.
- The parameter can have any positive value.
- If the number of elements to be deleted is equal to or greater than the size of the set. An empty set is returned.
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]): Unit = {
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.
Example 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]): Unit = {
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 3: 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]): Unit = {
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.