Open In App

Scala Set last() method with example

Last Updated : 18 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The last() method is utilized to return the last element of the set.

Method Definition: def last: A

Return Type: It returns the last element of the set.

Example #1:




// Scala program of last() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a set 
        val s1 = Set(1, 2, 3, 4
          
        // Applying last method 
        val result = s1.last
          
        // Display output
        println(result)
    


Output:

4

Example #2:




// Scala program of last() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a set 
        val s1 = Set("geeks", "for", "geeks", "classes"
          
        // Applying last method 
        val result = s1.last
          
        // Display output
        println(result)
    


Output:

classes


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads