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 s 1 = Set( 5 , 1 , 2 , 3 , 4 )
// Applying head method
val result = s 1 .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 s 1 = Set( 41 , 16 , 22 , 3 , 51 )
// Applying head method
val result = s 1 .head
// Display output
println(result)
}
} |
Output:
41