Scala Tutorial - Learn How To Use Intersect Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 14:35 pm

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)

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

  • 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 last function.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Technology and Finance Consultant with over 14 years of hands-on experience building large scale systems in the Financial (Electronic Trading Platforms), Risk, Insurance and Life Science sectors. I am self-driven and passionate about Finance, Distributed Systems, Functional Programming, Big Data, Semantic Data (Graph) and Machine Learning.
Other allaboutscala.com tutorials you may like: