Scala Tutorial - Learn How To Use Last Function With Examples

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:55 pm

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!

This concludes our tutorial on Learn How To Use Last Function With Examples and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

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

  • Review the tutorials on Mutable and Immutable collection data structures in Scala.

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.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on