Open In App

Scala Stream head() method with example

Last Updated : 26 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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

Method Definition: def head: A

Return Type: It returns the first element of the stated Stream.

Example #1:




// Scala program of head()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Creating a Stream
        val m1 = Stream(3, 6, 7, 4)
          
        // Applying head method
        val result = m1.head
          
        // Displays output
        println(result)
      
    }
}


Output:

3

Example #2:




// Scala program of head()
// method
   
// Creating object
object GfG
{  
     
    // Main method
    def main(args:Array[String])
    {
        
        // Creating a Stream
        val m1 = Stream(6, 8, 9, 12)
            
        // Applying head method
        val result = m1.head
             
        // Displays output
        println(result)
        
    }
}


Output:

6


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads