Scala Tutorial - Learn How To Use Find Function

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

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!

 

This concludes our tutorial on Learn How To Use Find Function 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 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

  • 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 flatMap functions.

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