Scala Tutorial - Learn How To Use ZipWithIndex Function With Examples

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

Overview

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

 

The zipWithIndex method will create a new collection of pairs or Tuple2 elements consisting of the element and its corresponding index.

 

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

def zipWithIndex: Iterable[(A, Int)]

The zipWithIndex 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 zip the donuts Sequence with their corresponding index using zipWithIndex method

In the previous tutorials, we have shown how to you can easily merge and un-merge Scala collections using the zip and unzip methods respectively. In addition, zipping and unzipping of collection is a fairly common practice with Tuples that Scala also provides an unzip3 method.

 

Sometimes though you may be interested in knowing the index position of a particular element in a collection. As such, Scala provides a convenient zipWithIndex method which does exactly this as shown below.


println("\nStep 2: How to zip the donuts Sequence with their corresponding index using zipWithIndex method")
val zippedDonutsWithIndex: Seq[(String, Int)] = donuts.zipWithIndex
zippedDonutsWithIndex.foreach{ donutWithIndex =>
 println(s"Donut element = ${donutWithIndex._1} is at index = ${donutWithIndex._2}")
}

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


Step 2: How to zip the donuts Sequence with their corresponding index using zipWithIndex method
Donut element = Plain Donut is at index = 0
Donut element = Strawberry Donut is at index = 1
Donut element = Glazed Donut is at index = 2

 

This concludes our tutorial on Learn How To Use ZipWithIndex 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 Sequence of donuts
  • How to zip the donuts Sequence with their corresponding index using zipWithIndex method

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

This tutorial concludes Chapter 8 Collection Function tutorials.

 

In the next chapter, I will go over asynchronous operations in Scala using Futures.

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: