Scala Tutorial - Learn How To Use Union Function With Examples

By Nadim Bahadoor | Last updated: March 16, 2018 at 15:13 pm

Overview

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

 

The union method takes a Set as parameter and will merge its elements with the elements from the current Set.

 

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

def union(that: GenSet[A]): This

The union method is a member of SetLike trait.

Steps

1. How to initialize a Set of donuts

The code below shows how to create a Set of donut elements 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

Let us create another Set of donut with elements 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 merge two Sets using union function

Using the union method, we can merge elements from the two donut sets as shown below.


println("\nStep 3: How to merge two Sets using union function")
println(s"Union of Sets donuts1 and donuts2 = ${donuts1 union donuts2}")
println(s"Union of Sets donuts2 and donuts1 = ${donuts2 union donuts1}")

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


Union of Sets donuts1 and donuts2 = Set(Vanilla Donut, Plain Donut, Chocolate Donut, Strawberry Donut, Glazed Donut)
Union of Sets donuts2 and donuts1 = Set(Vanilla Donut, Plain Donut, Chocolate Donut, Strawberry Donut, Glazed Donut)

4. How to merge two Sets using ++ function

Similar to Step 3 with using the union method, you can use the ++ method to combine elements from two sets.


println("\nStep 4: How to merge two Sets using ++ function")
println(s"Union of Sets donuts1 and donuts2 = ${donuts1 ++ donuts2}")
println(s"Union of Sets donuts2 and donuts1 = ${donuts2 ++ donuts1}")

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


Step 4: How to merge two Sets using ++ function
Union of Sets donuts1 and donuts2 = Set(Vanilla Donut, Plain Donut, Chocolate Donut, Strawberry Donut, Glazed Donut)
Union of Sets donuts2 and donuts1 = Set(Vanilla Donut, Plain Donut, Chocolate Donut, Strawberry Donut, Glazed Donut)

 

This concludes our tutorial on Learn How To Use Union 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 merge two Sets using union function
  • How to merge 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 the unzip 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: