Related Articles

Related Articles

Scala Set head() method with example
  • Last Updated : 18 Oct, 2019

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

5

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

41



My Personal Notes arrow_drop_up
Recommended Articles
Page :