Scala Tutorial - Learn How To Use Head Function With Examples
Overview
In this tutorial, we will learn how to use the head function with examples on collection data structures in Scala. The head function is applicable to both Scala's Mutable and Immutable collection data structures.
The head method will return the first element in the collection.
As per the Scala documentation, the definition of the head method is as follows:
def head: A
The head method is a member of the IterableLike 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 first element of the donut sequence
The code below shows how to access the first element in the donut sequence by using the zero index.
println("\nStep 2: How to access the first element of the donut sequence")
println(s"First element of donut sequence = ${donuts(0)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to access the first element of the donut sequence
First element of donut sequence = Plain Donut
3. How to access the first element of the donut sequence using the head method
The code below shows how to use the head method to access the first element of the donut sequence.
println("\nStep 3: How to access the first element of the donut sequence using the head method")
println(s"First element of donut sequence using head method = ${donuts.head}")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to access the first element of the donut sequence using the head method
First element of donut sequence using head method = Plain Donut
4. How to create an empty sequence
The code below shows how to represent an empty donut sequence by creating 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:
Step 4: How to create an empty sequence
Elements of donuts2 = List()
5. How to access the first element of the donut sequence using the headOption function
The code below shows how to use the headOption method to access the first element of an empty sequence in order to avoid unnecessary runtime exceptions on empty sequences.
println("\nStep 5: How to access the first element of the donut sequence using the headOption function")
println(s"First element of empty sequence = ${donuts2.headOption.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 first element of the donut sequence using the headOption function
First element of empty sequence = No donut was found!
NOTE:
- We also made use of the getOrElse() method which you can review from the tutorial on Option, Some and None.
Summary
In this tutorial, we went over the following:
- How to initialize a Sequence of donuts
- How to access the first element of the donut sequence
- How to access the first element of the donut sequence using the head method
- How to create an empty sequence
- How to access the first element of the donut sequence using the headOption 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 isEmpty function.