Open In App

Scala Set init() method with example

Last Updated : 18 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The init() method is utilized to find all the elements of the set except the last one.
Method Definition: def init: Set[A] Return Type: It returns all the elements of the set except the last one.
Example #1: Scala
// Scala program of init() 
// method 

// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a set 
        val s1 = Set(1, 2, 3, 4, 5) 
        
        // Applying init method 
        val result = s1.init
        
        // Display output
        println(result)
    } 
} 
Output:
Set(5, 1, 2, 3)
Example #2: Scala
// Scala program of init() 
// method 

// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a set 
        val s1 = Set(16, 22, 3, 41, 51) 
        
        // Applying init method 
        val result = s1.init
        
        // Display output
        println(result)
    } 
} 
Output:
Set(41, 22, 3, 16)

Next Article

Similar Reads