Find the last element of a list in scala - GeeksforGeeks

17 Apr, 2019
In Scala, list is defined under scala.collection.immutable package. A list is a collection of same type elements which contains immutable data. we generally use last function to print last element of a list.
Below are the examples to find the last element of a given list in Scala.
Simply print last element of a list// Scala program to find the last element of a given list import scala.collection.immutable._  // Creating object object GFG{     // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist: List[String] = List("Geeks", "For",                                 "geeks", "is", "best")               // Display the last value of mylist        println("Last element is: " + mylist.last)           } } Output:Last element is: best 
// Scala program to find the last element of a given list import scala.collection.immutable._  // Creating object object GFG{     // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist: List[String] = List("Geeks", "For",                                 "geeks", "is", "best")               // Display the last value of mylist        println("Last element is: " + mylist.last)           } } 
Output:
Last element is: best
 
Print last element of a list using for loop// Scala program to find the last element of a given list// using for loop import scala.collection.immutable._  // Creating object object GFG{     // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist: List[String] = List("Geeks", "For",                                         "geeks", "is",                                "a", "fabulous", "portal")          // Display the last value of mylist using for loop         for(element<-mylist.last)         {             print(element)         }     } } Output:portal 
// Scala program to find the last element of a given list// using for loop import scala.collection.immutable._  // Creating object object GFG{     // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist: List[String] = List("Geeks", "For",                                         "geeks", "is",                                "a", "fabulous", "portal")          // Display the last value of mylist using for loop         for(element<-mylist.last)         {             print(element)         }     } } 
Output:
portal
 
Print last element of a list using foreach loop// Scala program to find the last element of a given list// using foreach loop import scala.collection.immutable._  // Creating object object GFG{           // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist = List(1, 2, 3, 4, 5, 6)                            print("Original list is: ")        // Display the value of mylist using for loop         mylist.foreach{x:Int => print(x + " ") }                  // calling last function        println("\nLast element is: " + mylist.last)    } } Output:Original list is: 1 2 3 4 5 6 
Last element is: 6
// Scala program to find the last element of a given list// using foreach loop import scala.collection.immutable._  // Creating object object GFG{           // Main method     def main(args:Array[String])     {         // Creating and initializing immutable lists         val mylist = List(1, 2, 3, 4, 5, 6)                            print("Original list is: ")        // Display the value of mylist using for loop         mylist.foreach{x:Int => print(x + " ") }                  // calling last function        println("\nLast element is: " + mylist.last)    } } 
Output:
Original list is: 1 2 3 4 5 6 
Last element is: 6
Scala
Scala
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Type Casting in Scala
Scala Tutorial – Learn Scala with Step By Step Guide
Hello World in Scala
Operators in Scala
Class and Object in Scala
Scala String substring() method with example
Inheritance in Scala
Throw Keyword in Scala
Scala Constructors
Break statement in Scala