Scala SortedSet init() method with example

The init() method is utilized to find all the elements of the SortedSet except the last one.

Method Definition: def init: SortedSet[A]

Return Type: It returns all the elements of the SortedSet except the last one.



Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of init() 
// method 
import scala.collection.immutable.SortedSet 
  
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a SortedSet 
        val s1 = SortedSet(1, 2, 3, 4, 5
          
        // Applying init method 
        val result = s1.init
          
        // Display output
        println(result)
    

chevron_right


Output:

TreeSet(1, 2, 3, 4)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of init() 
// method 
import scala.collection.immutable.SortedSet 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a SortedSet 
        val s1 = SortedSet(16, 22, 3, 41, 51
          
        // Applying init method 
        val result = s1.init
          
        // Display output
        println(result)
    

chevron_right


Output:

TreeSet(3, 16, 22, 41)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.