Scala Tutorial - Learn How To Use Last Function With Examples
Overview
In this tutorial, we will learn how to use the last function with examples on collection data structures in Scala. The last function is applicable to both Scala's Mutable and Immutable collection data structures.
The last method will return the last element in a collection.
As per the Scala documentation, the definition of the last method is as follows:
def last: A
The last method is a member of the TraversableLike trait.
Steps
1. How to initialize a Sequence of donuts
The code below shows how to initialize a Sequence of donut elements where each element in the Sequence is of type String.
println("Step 1: How to initialize a Sequence of donuts")
val donuts: Seq[String] = Seq("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts = $donuts")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a Sequence of donuts
Elements of donuts = List(Plain Donut, Strawberry Donut, Glazed Donut)
2. How to access the last element of the donut sequence by index
The code below shows how to access the last element of the donut Sequence by using the last element's index.
println("\nStep 2: How to access the last element of the donut sequence by index")
println(s"Last element of donut sequence = ${donuts(donuts.size - 1)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to access the last element of the donut sequence by index
Last element of donut sequence = Glazed Donut
NOTE:
- Using the element's index is typically how you would access the last element in the Sequence in an imperative language such as Java or .NET
3. How to access the last element of the donut sequence by using the last function
The code below shows how to access the last element of the donut sequence in a more functional style by making use of the last method.
println("\nStep 3: How to access the last element of the donut sequence by using the last function")
println(s"Last element of donut sequence = ${donuts.last}")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to access the last element of the donut sequence by using the last function
Last element of donut sequence = Glazed Donut
4. How to create an empty sequence
The code below shows how to create an empty Sequence of type String.
println("\nStep 4: How to create an empty sequence")
val donuts2: Seq[String] = Seq.empty[String]
println(s"Elements of donuts2 = $donuts2")
You should see the following output when you run your Scala application in IntelliJ:
println("\nStep 4: How to create an empty sequence")
val donuts2: Seq[String] = Seq.empty[String]
println(s"Elements of donuts2 = $donuts2")
5. How to access the last element of the donut sequence using the lastOption function
The code below shows how to access the last element of a donut sequence by making use of the lastOption method. You should prefer to use the lastOption method if you are unsure that a last element exists in a collection.
println("\nStep 5: How to access the last element of the donut sequence using the lastOption function")
println(s"Last element of empty sequence = ${donuts2.lastOption.getOrElse("No donut was found!")}")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to access the last element of the donut sequence using the lastOption function
Last element of empty sequence = No donut was found!
Summary
In this tutorial, we went over the following:
- How to initialize a Sequence of donuts
- How to access the last element of the donut sequence by index
- How to access the last element of the donut sequence by using the last function
- How to create an empty sequence
- How to access the last element of the donut sequence using the lastOption function
Tip
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to use map function.