Updated April 11, 2023
Introduction to Scala Zip
The Scala Zip method is a member of the trait Iterable which takes a collection as a parameter and merges the elements with a new collection creating paired elements in a new collection. It is applicable to both Mutable as well as Immutable collection data.
It’s just like zipping files, zipping the files merges it in one zipped folder in the same way Scala Zip Function merges the collection elements with there values. So basically Scala Zip can be termed as combining two collection objects together.
Syntax and Parameters
Scala Zip belongs to a member of the class Abstract Iterator.
The Scala Zip comes with the below syntax:-
defzip[Y](that: IterableOnce[Y]): List[(X, Y)]
Parameters used:
Y: It is the type of second half of the returned pair
The result is consists of new iterable collections consisting of both the collection for which the Zip function is applied.
Working of Scala Zip
Scala Zip function takes up two collections and takes the other collection as a parameter and merge the element with the first one. It creates a new collection of pairs from both the collection.
It maps the first element of the collection with the first element of other collection and produces the resulting pair. If the length is the same the same element paired zip is produced and if the length is not the same the collection with minimum length is taken and the pairing is done till that length.
Examples of Scala Zip
Let us check the working with the help of some examples:-
Example #1
Creating two list elements and using Zip function for the same length.
Code:
object Main extends App {
val a = List(1,2,3,5,6)
val b = List(3,4,5)
val result = a.zip(b)
println(result)
}
object Main extends App {
val a = List(1,2,3,5,6)
val b = List(3,4,5,6,7)
val result = a.zip(b)
println(result)
}
Here from the above example, we can see that by using the Zip function we can create a new paired List.
object Main extends App {
val a = List(1,2,3,5,6)
val b = List(3,4,5,6,7)
val c = a.zip(b)
val d = b.zip(a)
println(c)
println(d)
}
Just by reversing the list will reverse the order of pairing as shown in val d.
Example #2
Creating two list elements and using Zip function for different lengths.
Here the length of the list changes so the minimum length is taken as making a new collection.
Code:
Object Main extends App {
val a = List(1,2,3,4)
val b = List(3,4,5)
val c = a.zip(b)
println(c)
}
Since the list b length is lesser than a, so after using the ZIP function we will get paired list for length 3.
Using unzip function we can unzip the object from the Zip method. So basically it distributes the list again to its initial object elements.
Let us check that with an example:-
object Main extends App {
val a = List(1,2,3,4)
val b = List(3,4,5)
val d = b.zip(a)
val e = d.unzip
println(e)
}
Example #3
Zipping List of type String.
We can zip all the data types in a list with the help of Zip function. Let us check zip function for List type String with an example:-
Code:
object Main extends App {
val a = List("Arpit", "Anand")
val b = List("Ind", "Eur")
val c = a.zip(b)
println(c)
}
Even we can also use the Map operation over the Zipped collection and use it for further operational purpose. Let’s check it with an Example:-
Suppose we have two lists and we have to use the Zip function over there, now we want the result to be the sum of the values that are paired so for this we can use the map the function over the Zip function. Since Zip value makes a Tuple so we can extract the Tuples with the help of pattern matching in Scala.
object Main extends App {
val a = List(1,2,3,5)
val b = List(2,5,6,7)
val c = a.zip(b)
println(c.map{case(x,y) => x+y})
}
here we can see that the sum of the list is returned in the result set.
So from the above article and examples, we saw how we can create and use the Zip function and how to map operations can be done with the Zipped values.
Conclusion
From the above article we saw how scala Zip function works in Scala, we also see the syntax and parameter used for the Scala Zip function, with the help of examples we saw how ZIP function works and how it helps in creating a new paired collection with the help of collection over scala.
We also saw how to unzip function is use to unzip the object we zipped in. So Scala Zip Function is good for the Object-Oriented Programming approach.
Recommended Articles
We hope that this EDUCBA information on “Scala Zip” was beneficial to you. You can view EDUCBA’s recommended articles for more information.