Scala Tutorial - Learn How To Use Zip Function With Examples
Overview
In this tutorial, we will learn how to use the zip function with examples on collection data structures in Scala. The zip function is applicable to both Scala's Mutable and Immutable collection data structures.
The zip method takes another collection as parameter and will merge its elements with the elements of the current collection to create a new collection consisting of pairs or Tuple2 elements from both collections.
As per the Scala documentation, the definition of the zip method is as follows:
def zip[B](that: GenIterable[B]): Iterable[(A, B)]
The zip method is a member of IterableLike trait.
Steps
1. How to initialize a Sequence of donuts
The code below shows how to create a Sequence of type String to represent a donut collection.
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 initialize a Sequence of donut prices
Similar to Step 1, we will initialize another Sequence but of type Double to represent donut prices.
println("\nStep 2: How to initialize a Sequence of donut prices")
val donutPrices: Seq[Double] = Seq(1.5, 2.0, 2.5)
println(s"Elements of donut prices = $donutPrices")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to initialize a Sequence of donut prices
Elements of donut prices = List(1.5, 2.0, 2.5)
3. How to use zip method to zip two collections
Using the zip method, you can easily merge two collections into a single one which will consist of pairs or elements as Tuples. As such, let's zip the donuts and donutPrices collections into a single collection using the zip method.
println("\nStep 3: How to use zip method to zip two collections")
val zippedDonutsAndPrices: Seq[(String, Double)] = donuts zip donutPrices
println(s"Zipped donuts and prices = $zippedDonutsAndPrices")
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to use zip method to zip two collections
Zipped donuts and prices = List((Plain Donut,1.5), (Strawberry Donut,2.0), (Glazed Donut,2.5))
NOTE:
- The elements of the resulting zipped collections are tuples with donut names along with their corresponding prices.
4. How to use unzip method to un-merge a zipped collections
In the Learn How To Use Unzip Function With Examples tutorial, we showed how to use the unzip method. The example below is a review on using unzip method to un-merge the donut names and prices collections.
println("\nStep 4: How to use unzip method to un-merge a zipped collections")
val unzipped: (Seq[String], Seq[Double]) = zippedDonutsAndPrices.unzip
println(s"Donut names unzipped = ${unzipped._1}")
println(s"Donut prices unzipped = ${unzipped._2}")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to use unzip method to un-merge a zipped collections
Donut names unzipped = List(Plain Donut, Strawberry Donut, Glazed Donut)
Donut prices unzipped = List(1.5, 2.0, 2.5)
Summary
In this tutorial, we went over the following:
- How to initialize a Sequence of donuts
- How to initialize a Sequence of donut prices
- How to use zip method to zip two collections
- How to use unzip method to un-merge a zipped collections
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 the zipWithIndex function.