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 m 1 = Stream( 3 , 6 , 7 , 4 )
// Applying head method
val result = m 1 .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 m 1 = Stream( 6 , 8 , 9 , 12 )
// Applying head method
val result = m 1 .head
// Displays output
println(result)
}
} |
Output:
6