Scala Tutorial - Learn How To Use Diff Function
Overview
In this tutorial, we will learn how to use the diff function on collection data structures in Scala. The diff function is applicable to both Scala's Mutable and Immutable collection data structures.
The diff method takes another Set as its parameter and uses it to find the elements that are different from the current Set compared to another Set.
As per the Scala documentation, the definition of the diff method is as follows:
def diff(that: GenSet[A]): This
The diff method is a member of the SetLike trait.
Steps
1. How to initialize a Set containing 3 donuts
The code below shows how to initialize a Set containing 3 Donut elements of type String.
println("Step 1: How to initialize a Set containing 3 donuts")
val donutBasket1: Set[String] = Set("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donutBasket1 = $donutBasket1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a Set containing 3 donuts
Elements of donutBasket1 = Set(Plain Donut, Strawberry Donut, Glazed Donut)
2. How to initialize a Set containing 2 donuts
The code below shows how to initialize a Set containing 2 Donut elements of type String.
println("\nStep 2: How to initialize a Set containing 2 donuts")
val donutBasket2: Set[String] = Set("Glazed Donut", "Vanilla Donut")
println(s"Elements of donutBasket2 = $donutBasket2")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to initialize a Set containing 2 donuts
Elements of donutBasket2 = Set(Glazed Donut, Vanilla Donut)
3. How to find the difference between two Sets using the diff function
The code below shows how to find the difference between two Sets - more precisely the difference between the donutBasket1 from Step 1 to donutBasket2 from Step 2.
println("\nStep 3: How to find the difference between two Sets using the diff function")
val diffDonutBasket1From2: Set[String] = donutBasket1 diff donutBasket2
println(s"Elements of diffDonutBasket1From2 = $diffDonutBasket1From2")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to find the difference between two Sets using the diff function
Elements of diffDonutBasket1From2 = Set(Plain Donut, Strawberry Donut)
NOTE:
- The element Glazed Donut was removed as it was common in both Sets.
4. How to find the difference between two Sets using the diff function
The code below shows how to find the difference between two Sets - more precisely the difference between donutBaset2 from Step 2 to donutBaset1 from Step 1.
println("\nStep 4: How to find the difference between two Sets using the diff function")
val diffDonutBasket2From1: Set[String] = donutBasket2 diff donutBasket1
println(s"Elements of diff DonutBasket2From1 = $diffDonutBasket2From1")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to find the difference between two Sets using the diff function
Elements of diff DonutBasket2From1 = Set(Vanilla Donut)
NOTE:
- The element Glazed Donut was removed as it was common in both Sets.
5. How to find the difference between two Sets using the --
The code below shows how to find the difference between 2 Sets using the -- operator which will yield similar results as with the diff method.
println("\nStep 5: How to find the difference between two Sets using the --")
println(s"Difference between donutBasket1 and donutBasket2 = ${donutBasket1 -- donutBasket2}")
println(s"Difference between donutBasket2 and donutBasket1 = ${donutBasket2 -- donutBasket1}")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to find the difference between two Sets using the --
Difference between donutBasket1 and donutBasket2 = Set(Plain Donut, Strawberry Donut)
Difference between donutBasket2 and donutBasket1 = Set(Vanilla Donut)
Summary
In this tutorial, we went over the following:
- How to initialize a Set containing 3 donuts
- How to initialize a Set containing 2 donuts
- How to find the difference between two Sets using the diff function
- How to find the difference between two Sets using the --
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 drop function.