Open In App

Scala Set head() method with example

The head() method is utilized to display the first element of the set.

Method Definition: def head: A



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

Example #1:




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

Output:

5

Example #2:




// Scala program of head() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a set 
        val s1 = Set(41, 16, 22, 3, 51
          
        // Applying head method 
        val result = s1.head
          
        // Display output
        println(result)
    

Output:
41

Article Tags :