Scala Tutorial - Learn How To Use Find Function
Overview
In this tutorial, we will learn how to use the find function on collection data structures in Scala. The find function is applicable to both Scala's Mutable and Immutable collection data structures.
The find method takes a predicate function as parameter and uses it to find the first element in the collection which matches the predicate. It returns an Option and as such it may return a None for the case where it does not match any elements in the collection with the predicate function.
As per the Scala documentation, the definition of the find method is as follows:
def find(p: (A) ⇒ Boolean): Option[A]
The find 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 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 find a particular element in the sequence using the find function
The code below shows how to use the find method to find a particular element in the sequence - more precisely to find for a Plain Donut element in the donut sequence.
println("\nStep 2: How to find a particular element in the sequence using the find function")
val plainDonut: Option[String] = donuts.find(donutName => donutName == "Plain Donut")
println(s"Find Plain Donut = ${plainDonut.get}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to find a particular element in the sequence using the find function
Find Plain Donut = Plain Donut
NOTE:
- The find method returns an Option type and as such it returned an Option of type String in the above example.
3. How to find element Vanilla Donut which does not exist in the sequence using the find function
The code below shows how to use the find method to find a Vanilla Donut element in the donut sequence. Note however that there is no Vanilla Donut element in the donut sequence which we created in Step 1.
println("\nStep 3: How to find element Vanilla Donut which does not exist in the sequence using the find function")
val vanillaDonut: String = donuts.find(_ == "Vanilla Donut").get
println(s"Find Vanilla Donuts = $vanillaDonut")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to find element Vanilla Donut which does not exist in the sequence using the find function
Exception in thread "main" java.util.NoSuchElementException: None.get
NOTE:
- Since the find method returns an Option, you should be careful with simply calling .get otherwise you may get the java.util.NoSuchElementException: None.get exception
4. How to find element Vanilla Donut using the find function and getOrElse
The code below shows how to use the find method which returns an Option type and make use of the getOrElse method to avoid getting java.util.NoSuchElementException: None.get exception for the cases when the find method does not match any elements.
println("\nStep 4: How to find element Vanilla Donut using the find function and getOrElse")
val vanillaDonut2: String = donuts.find(_ == "Vanilla Donut").getOrElse("Vanilla Donut was not found!")
println(s"Find Vanilla Donuts = $vanillaDonut2")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to find element Vanilla Donut using the find function and getOrElse
Find Vanilla Donuts = Vanilla Donut was not found!
Summary
In this tutorial, we went over the following:
- How to initialize a Sequence of donuts
- How to find a particular element in the sequence using the find function
- How to find element Vanilla Donut which does not exists in the sequence using the find function
- How to find element Vanilla Donut using the find function and getOrElse
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 flatMap functions.