- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the last element of a list in scala
Suppose we have a list in Scala, this list is defined under scala.collection.immutable package. As we know, a list is a collection of same type elements which contains immutable (cannot be changed) data. We generally apply last function to show last element of a list.
Using last keyword
The following Scala code is showing how to print the last element stored in a list of Scala.
Example
import scala.collection.immutable._ object HelloWorld { def main(args: Array[String]) { val temp_list: List[String] = List("Hello", "World", "SCALA", "is", "awesome") println("Elements of temp_list: " + temp_list.last) } }
Output
$scala HelloWorld Elements of temp_list: awesome
Using For Loop
The following Scala code is showing how to print the last element stored in a list of Scala using for loop.
Example
import scala.collection.immutable._ object HelloWorld { def main(args: Array[String]) { val temp_list: List[String] = List("Hello", "World", "SCALA", "is", "awesome") for(element<-temp_list.last) { print(element) } } }
Output
$scala HelloWorld awesome
Using Foreach Loop
The following Scala code is showing how to print the elements using for-each loop and display the last element.
Example
import scala.collection.immutable._ object HelloWorld { def main(args: Array[String]) { val temp_list: List[String] = List("Hello", "World", "SCALA", "is", "awesome") temp_list.foreach{x:String => print(x + ", ") } println("
Last element is: " + temp_list.last) } }
Output
$scala HelloWorld Hello, World, SCALA, is, awesome, Last element is: awesome
- Related Articles
- How to find the last occurrence of an element in a Java List?
- How to get the last element of a list in Python?
- Last occurrence of some element in a list in Python
- Python How to get the last element of list
- How to get the second-to-last element of a list in Python?
- Move last element to front of a given Linked List in C++
- How to delete last element from a List in C++ STL
- Find the second last node of a linked list in single traversal in C++
- How to find last occurence of element in the array in TypeScript?
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Program to find the K-th last node of a linked list in Python
- Python - Given a list of integers that represents a decimal value, increment the last element by 1
- Java Program to Add Element at First and Last Position of a Linked list
- Find a specific element in a C# List

Advertisements