In the tutorial, we will show how to use zip() and zipWithNext() methods of Kotlin List collection.
Contents
I. zip() method
1. zip()
Use below method signatures:
infix fun <T, R> Iterable<T>.zip( other: Array<out R> ): List<Pair<T, R>> infix fun <T, R> Iterable<T>.zip( other: Iterable<R> ): List<Pair<T, R>> |
-> 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<String>) { 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 <T, R, V> Iterable<T>.zip( other: Array<out R>, transform: (a: T, b: R) -> V ): List<V> fun <T, R, V> Iterable<T>.zip( other: Iterable<R>, transform: (a: T, b: R) -> V ): List<V> |
-> 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<String>) { 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 <T, R, V> Iterable<T>.zip(other: Iterable<R>, transform: (a: T, b: R) -> V): List<V> 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 <T> Iterable<T>.zipWithNext(): List<Pair<T, T>> |
-> Returns a list of pairs of each two adjacent elements in this collection.
Sample:
fun main(args: Array<String>) { val numbers = listOf(1, 2, 3) val zipedList = numbers.zipWithNext() println(zipedList) // -> [(1, 2), (2, 3)] } |
2. zipWithNext() with transform function
Method signature:
fun <T, R> Iterable<T>.zipWithNext( transform: (a: T, b: T) -> R ): List<R> |
-> 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<String>) { val numbers = listOf(1, 2, 3) val zipWithNextList = numbers.zipWithNext{a, b -> "${a} + ${b} = ${a + b}"} println(zipWithNextList) // -> [1 + 2 = 3, 2 + 3 = 5] } |
Last updated on February 6, 2020.