Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Scala Iterator toSet() method with example

  • Last Updated : 26 Jul, 2019

The toSet() method belongs to the concrete value member of the class Abstract Iterator. It is defined in the class IterableOnceOps.

Method Definition: def toSet[B >: A]: immutable.Set[B]

Return Type: It returns a set from the stated collection.

Example #1:




// Scala program of toSet()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a Iterator 
        val iter = Iterator(5, 6, 8, 9)
          
        // Applying toSet method 
        val result = iter.toSet
          
        // Displays output
        println(result)
      
    }
}
Output:
Set(5, 6, 8, 9)

Example #2:




// Scala program of toSet()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating an empty Iterator 
        val iter = Iterator()
          
        // Applying toSet method 
        val result = iter.toSet
          
        // Displays output
        println(result)
      
    }
}
Output:
Set()

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!