Scala Tutorial - Learn How To Use Exists Function
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:
- The predicate function is a Value Function.
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
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
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.