Scala Tutorial - Learn How To Use Intersect Function With Examples
Overview
In this tutorial, we will learn how to use the intersect function with examples on collection data structures in Scala. The intersect function is applicable to both Scala's Mutable and Immutable collection data structures.
The intersect method will find the common elements between two Sets.
As per the Scala documentation, the definition of the intersect method is as follows:
def intersect(that: GenSet[A]): Repr
The intersect method is a member of the GenSetLike trait.
Steps
1. How to initialize a Set of donuts
The code below shows how to initialize a Set of donut elements where each element in the Set is of type String.
println("Step 1: How to initialize a Set of donuts")
val donuts1: Set[String] = Set("Plain Donut", "Strawberry Donut", "Glazed Donut")
println(s"Elements of donuts1 = $donuts1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize a Set of donuts
Elements of donuts1 = Set(Plain Donut, Strawberry Donut, Glazed Donut)
2. How to initialize another Set of donuts
The code below shows how to initialize another Set of donut elements where each element in the Set is of type String.
println("\nStep 2: How to initialize another Set of donuts")
val donuts2: Set[String] = Set("Plain Donut", "Chocolate Donut", "Vanilla Donut")
println(s"Elements of donuts2 = $donuts2")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to initialize another Set of donuts
Elements of donuts2 = Set(Plain Donut, Chocolate Donut, Vanilla Donut)
3. How to find the common elements between two Sets using intersect function
The code below shows how to the common elements between two Sets by using the intersect function.
println("\nStep 3: How to find the common elements between two Sets using intersect function")
println(s"Common elements between donuts1 and donuts2 = ${donuts1 intersect donuts2}")
println(s"Common elements between donuts2 and donuts1 = ${donuts2 intersect donuts1}")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to find the common elements between two Sets using intersect function
Common elements between donuts1 and donuts2 = Set(Plain Donut)
Common elements between donuts2 and donuts1 = Set(Plain Donut)
4. How to find the common elements between two Sets using & function
The code below shows how to find the common elements between two Sets by using the & function.
println("\nStep 4: How to find the common elements between two Sets using & function")
println(s"Common elements between donuts1 and donuts2 = ${donuts1 & donuts2}")
println(s"Common elements between donuts2 and donuts1 = ${donuts2 & donuts1}")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to find the common elements between two Sets using & function
Common elements between donuts1 and donuts2 = Set(Plain Donut)
Common elements between donuts2 and donuts1 = Set(Plain Donut)
Summary
In this tutorial, we went over the following:
- How to initialize a Set of donuts
- How to initialize another Set of donuts
- How to find the common elements between two Sets using intersect function
- How to find the common elements between two Sets using & function
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 last function.