How to drop elements in Scala Set collection

 78 total views,  2 views today

Welcome to Scala series. In this tutorial, you will learn How to drop elements in Scala Set collection.

Basics of Scala Set

  • Set is a collection to store values that don’t have/allow duplicate values. Always Set will have unique elements.
  • Set is Iterable and inherits all functionality of the iterable interface.
  • There are two variants of Set in Scala
    • Immutable Set(default)
    • Mutable Set
  • By default, Scala will use Immutable Set. In any case, if a mutable Set is needed, then we need to explicitly import mutable Set.
  • Immutable Set allows us to apply a function on Set but resulting in the creation of a new Set every time. It doesn’t allow us to change the source. On the other hand, the mutable Set allows modifying the source itself.

I have written a separate post on Scala immutable and mutable collection. So please read before you proceed.


Objective

In Scala, How to drop N elements in Set using drop() method.


Solution

drop() operation is a useful operation of the Scala Set which allows us to drop first N elements from the Set or collection. When you call this operation on a Set, it returns a new collection by dropping the first N elements. i.e., drop first N elements from the left.

Syntax

def drop(n: Int): Set[A]

Selects all elements except first n ones.

Example

In this example, we have created a Set with few string values and used drop() method to drop first two elements.

/**
 * How to drop elements in Scala Set collection
 */
package com.scala

import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //create a Set
    var set = Set("staticreference", "like", "share", "feedback")

    var N = 3;

    //Drop and Print
    println(set.drop(2))
  }
}

Output

Set(share, feedback)

Example 2

In this example, the size of the Set is 5 but N = 8 which is greater than the size. So drop() will return the empty sequence or collection.

/**
 * How to drop elements in Scala Set collection
 */
package com.scala

import java.io.PrintWriter
import java.io.File
import java.io.FileWriter

object StaticReference {

  def main(args: Array[String]): Unit = {

    //create a Set
    var set = Set("staticreference", "like", "share", "feedback", "subscribe")

    var N = 7;

    //Drop and Print
    set.drop(N).foreach(println)
  }
}

<Empty output>

ReferencesScala Set drop() operation

Would you like to learn more about Scala features, please check out Scala – a complete guide series. In this tutorial guide, you will find useful Scala functions that can be applied to the collection.

I hope you like this tutorial and you were able to understand Scala Set drop operation with examples.

Thanks for reading and please give us a thumb up and comment below.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.