Sunday 13 June 2021

Kotlin Collection - zip & unzip function

zip function builds one new list of paired elements from two existing arrays. Zipping transformation is useful to combine two array values. The final result is equal to the length of the smaller array if both arrays have different size. The extra elements of the larger array are not included in the final list.

Similarly, to do the reverse transformation, i.e. unzipping, unzip() method is called. In this post, Please refer to the below code snippet for the same. 

In the below example we are associating the states with their corresponding registration code, and in the unzip we are doing reverse the things.   

Here, while doing the zip we are getting the list of pairs, and for the unzip we are using the destructuing it with two separate list of strings. 

No comments: