Kotlin List – zip() + zipWithNext()

In the tutorial, we will show how to use zip() and zipWithNext() methods of Kotlin List collection.

I. zip() method

1. zip()

Use below method signatures:


infix fun  Iterable.zip(
    other: Array
): List>
infix fun  Iterable.zip(
    other: Iterable
): List>

-> Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection.

Sample:


package com.javasampleapproach.kotlin.list.zip

fun main(args: Array) {
	val numbers = listOf(1, 2, 3)
	val strings = listOf("one", "two","three")
	val numStrList = numbers.zip(strings)
	println(numStrList)
	
	// -> [(1, one), (2, two), (3, three)]
}

2. zip() with transform function

Use below method signatures:


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

-> Returns a list of values built from elements of both collections with same indexes using provided transform. List has length of shortest collection.

Sample:


package com.javasampleapproach.kotlin.list.zip

data class Address(
	val street: String,
	val postcode: String
)
 
data class Customer(
	val firstName: String,
	val lastName: String,
	val age: Int
)
 
data class Person(
	val fullname: String,
	val age: Int,
	val address: Address
)

fun main(args: Array) {
	val customers = listOf(Customer("Jack", "Davis", 25),
								Customer("Mary", "Taylor", 37),
								Customer("Peter", "Thomas",17),
								Customer("Amos", "Nelson",23),
								Customer("Craig", "White",45),
								Customer("Laura", "Lewis", 32),
								Customer("Steven", "Harris", 39),
								Customer("Paul", "Moore", 18),
								Customer("Mary", "Cook", 61))
	
	val addresses = listOf(Address("NANTERRE CT", "77471"),
								Address("W NORMA ST", "77009"),
								Address("S NUGENT AVE", "77571"),
								Address("E NAVAHO TRL", "77449"),
								Address("AVE N", "77587"),
								Address("NANTERRE CT", "77471"),
								Address("S NUGENT AVE", "77571"),
								Address("E NAVAHO TRL", "77449"),
								Address("S NUGENT AVE", "77571"))
	
	// inline fun  Iterable.zip(other: Iterable, transform: (a: T, b: R) -> V): List
	val people = customers.zip(addresses, {customer, address
								-> Person(customer.firstName + customer.lastName,
											customer.age,
											address)})
	
	people.forEach{println(it)}
	
	/*
		Person(fullname=JackDavis, age=25, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=MaryTaylor, age=37, address=Address(street=W NORMA ST, postcode=77009))
		Person(fullname=PeterThomas, age=17, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=AmosNelson, age=23, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=CraigWhite, age=45, address=Address(street=AVE N, postcode=77587))
		Person(fullname=LauraLewis, age=32, address=Address(street=NANTERRE CT, postcode=77471))
		Person(fullname=StevenHarris, age=39, address=Address(street=S NUGENT AVE, postcode=77571))
		Person(fullname=PaulMoore, age=18, address=Address(street=E NAVAHO TRL, postcode=77449))
		Person(fullname=MaryCook, age=61, address=Address(street=S NUGENT AVE, postcode=77571))
 	*/
}

II. zipWithNext() method

1. zipWithNext()

Method signature:


fun  Iterable.zipWithNext(): List>

-> Returns a list of pairs of each two adjacent elements in this collection.

Sample:


fun main(args: Array) {
	val numbers = listOf(1, 2, 3)
	val zipedList = numbers.zipWithNext()
	println(zipedList)
	// -> [(1, 2), (2, 3)]
}

2. zipWithNext() with transform function

Method signature:


fun  Iterable.zipWithNext(
    transform: (a: T, b: T) -> R
): List

-> Returns a list containing the results of applying the given transform function to an each pair of two adjacent elements in this collection.

Sample:


fun main(args: Array) {
	val numbers = listOf(1, 2, 3)
    val zipWithNextList = numbers.zipWithNext{a, b -> "${a} + ${b} = ${a + b}"}
    println(zipWithNextList)
    // -> [1 + 2 = 3, 2 + 3 = 5]
}


By grokonez | February 25, 2018.

Last updated on April 13, 2021.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*