Open In App

Scala Set init() method with example

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 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 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)

Article Tags :