The take() method belongs to the value member of the class List. It is utilized to take the first n elements from the list.
Method Definition: deftake(n: Int): List[A]
Where, n is the number of elements to be taken from the list.
Return Type:It returns a list containing only the first n elements from the stated list or returns the whole list if n is more than the number of elements in the given list.
Example #1:
object GfG
{
def main(args : Array[String])
{
val list = List( 1 , 2 , 3 , 4 , 5 , 6 , 7 )
val result = list.take( 4 )
println(result)
}
}
|
Example #2:
object GfG
{
def main(args : Array[String])
{
val list = List( "a" , "b" , "c" , "d" , "e" , "f" )
val result = list.take( 4 )
println(result)
}
}
|