Home »
Scala
How to get the first element of the list in Scala?
By IncludeHelp Last updated : October 20, 2024
List
A list is a linear data structure. It is a collection of elements of the same data types.
Scala libraries have many functions to support the functioning of lists. Methods like isempty, head, tail, etc provide basic operations on list elements.
Getting first element of the list
You can access the first element of the list in Scala using the head method form the list.
Scala Program to Get First Elements of a List
object MyObject{
def main(args:Array[String]) {
val myBikes = List("ThunderBird 350", "Yamaha R3", "BMW S1000R", "Iron 883")
println("list of my Bikes : " + myBikes)
println("First Bike from the list is: " + myBikes.head)
}
}
Output
list of my Bikes : List(ThunderBird 350, Yamaha R3, BMW S1000R, Iron 883)
First Bike from the list is: ThunderBird 350