Scala Tutorial - Learn How To Use Exists Function

By Nadim Bahadoor | Last updated: July 25, 2017 at 15:00 pm

Overview

In this tutorial, we will learn how to use the exists function on collection data structures in Scala. The exists function is applicable to both Scala's Mutable and Immutable collection data structures.

 

The exists method takes a predicate function and will use it to find the first element in the collection which matches the predicate.

 

As per the Scala documentation, the definition of the exists method is as follows:


def exists(p: (A) ⇒ Boolean): Boolean

 

The exists 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 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 check if a particular element exists in the sequence using the exists function

The code below shows how to use the exists method to find if a particular element exists in a sequence - more precisely if donut element Plain Donut exists in the donut sequence.


println("\nStep 2: How to check if a particular element exists in the sequence using the exists function")
val doesPlainDonutExists: Boolean = donuts.exists(donutName => donutName == "Plain Donut")
println(s"Does Plain Donut exists = $doesPlainDonutExists")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to check if a particular element exists in the sequence using the exists function
Does Plain Donut exists = true

 

3. How to declare a predicate value function for the exists function

The code below shows how to declare a predicate value function to find if a Plain Donut element exists in a sequence.



println("\nStep 3: How to declare a predicate value function for the exists function")
val plainDonutPredicate: (String) => Boolean = (donutName) => donutName == "Plain Donut"
println(s"Value function plainDonutPredicate = $plainDonutPredicate")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to declare a predicate value function for the exists function
Value function plainDonutPredicate = <function1>

NOTE:

4. How to find element Plain Donut using the exists function and passing through the predicate function from Step 3

The code below shows how to call the exists method and pass-through the value predicate function from Step 3 to find if a Plain Donut element exists in the donut sequence.



println("\nStep 4: How to find element Plain Donut using the exists function and passing through the predicate function from Step 3")
println(s"Does Plain Donut exists = ${donuts.exists(plainDonutPredicate)}")

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to find element Plain Donut using the exists function and passing through the predicate function from Step 3
Does Plain Donut exists = true

 

5.How to declare a predicate def function for the exists function

The code below shows how to a predicate def function to find if a Plain Donut element exists in a sequence.



println("\nStep 5: How to declare a predicate def function for the exists function")
def plainDonutPredicateFunction(donutName: String): Boolean = donutName == "Plain Donut"

6. How to find element Plain Donut using the exists function and passing through the predicate def function from Step 5

The code below shows how to call the exists method and pass-through the def predicate function from Step 5 to find if a Plain Donut element exists in the donut sequence.



println("\nStep 6: How to find element Plain Donut using the exists function and passing through the predicate function from Step 5")
println(s"Does plain Donut exists = ${donuts.exists(plainDonutPredicateFunction(_))}")

You should see the following output when you run your Scala application in IntelliJ:


Step 6: How to find element Plain Donut using the exists function and passing through the predicate function from Step 5
Does plain Donut exists = true

This concludes our tutorial on Learn How To Use Exists 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 check if a particular element exists in the sequence using the exists function
  • How to declare a predicate value function for the exists function
  • How to find element Plain Donut using the exists function and passing through the predicate function from Step 3
  • How to declare a predicate def function for the exists function
  • How to find element Plain Donut using the exists function and passing through the predicate def function from Step 5

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 filter and filterNot 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