Kotlin Tutorials

Kotlin Set zip()
Syntax & Examples

Set.zip() extension function

The zip() extension function in Kotlin returns a list of pairs built from the elements of this set and the other collection or array with the same index. It can also return a list of values built from the elements of this set and the other collection or array using a provided transform function.


Syntax of Set.zip()

There are 4 variations for the syntax of Set.zip() extension function. They are:

1.
infix fun <T, R> Set<T>.zip(other: Array<out R>): List<Pair<T, R>>

This extension function returns a list of pairs built from the elements of this set and the other array with the same index. The returned list has the length of the shortest collection.

Returns value of type List<Pair<T, R>>.

2.
fun <T, R, V> Set<T>.zip(other: Array<out R>, transform: (a: T, b: R) -> V): List<V>

This extension function returns a list of values built from the elements of this set and the other array with the same index using the provided transform function applied to each pair of elements. The returned list has the length of the shortest collection.

Returns value of type List<V>.

3.
infix fun <T, R> Set<T>.zip(other: Iterable<R>): List<Pair<T, R>>

This extension function returns a list of pairs built from the elements of this set and the other collection with the same index. The returned list has the length of the shortest collection.

Returns value of type List<Pair<T, R>>.

4.
fun <T, R, V> Set<T>.zip(other: Iterable<R>, transform: (a: T, b: R) -> V): List<V>

This extension function returns a list of values built from the elements of this set and the other collection with the same index using the provided transform function applied to each pair of elements. The returned list has the length of the shortest collection.

Returns value of type List<V>.



✐ Examples

1 Zipping a set with an array

Using zip() to create a list of pairs from a set and an array.

For example,

  1. Create a set of integers.
  2. Create an array of strings.
  3. Use zip() to pair elements of the set with elements of the array.
  4. Print the resulting list of pairs.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3)
    val words = arrayOf("one", "two", "three")
    val result = numbers zip words
    println(result)
}

Output

[(1, one), (2, two), (3, three)]

2 Zipping a set with a list using a transform function

Using zip() to create a list of strings from a set and a list, applying a transform function.

For example,

  1. Create a set of integers.
  2. Create a list of strings.
  3. Use zip() with a transform function to combine elements of the set and list into formatted strings.
  4. Print the resulting list of transformed values.

Kotlin Program

fun main() {
    val numbers = setOf(1, 2, 3)
    val words = listOf("one", "two", "three")
    val result = numbers.zip(words) { a, b -> "$a -> $b" }
    println(result)
}

Output

[1 -> one, 2 -> two, 3 -> three]

3 Zipping a set with another set

Using zip() to create a list of pairs from two sets.

For example,

  1. Create two sets of integers.
  2. Use zip() to pair elements of the two sets.
  3. Print the resulting list of pairs.

Kotlin Program

fun main() {
    val set1 = setOf(1, 2, 3)
    val set2 = setOf(4, 5, 6)
    val result = set1 zip set2
    println(result)
}

Output

[(1, 4), (2, 5), (3, 6)]

Summary

In this Kotlin tutorial, we learned about zip() extension function of Set: the syntax and few working examples with output and detailed explanation for each example.