Join multiple lists in Kotlin
This article explores different ways to join multiple lists in Kotlin.
1. Using flatMap() function
The flatMap() function returns a flattened list of elements yielded from invoking the specified transform function on each collection element. The idea is to create a list of lists, and flatten each list using the flatMap() function, and collect all the elements in a new list. This would translate to a simple code below:
|
1 2 3 4 5 6 7 8 9 10 11 |
fun main() { val first = listOf("Ruby on Rails", "NodeJS", "Go") val second = listOf("PHP", "Kotlin") val third = listOf("Ruby", "Perl") val result = listOf(first, second, third) .flatMap(List<String>::toList) .toList() println(result) } |
Output:
[Ruby on Rails, NodeJS, Go, PHP, Kotlin, Ruby, Perl]
2. Using addAll() function
Another solution is to create a new list and add elements of each list to it using the addAll() function, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun <T> concat(vararg lists: List<T>): List<T> { val newList: MutableList<T> = mutableListOf() for (list in lists) { newList.addAll(list) } return newList } fun main() { val first = listOf("Ruby on Rails", "NodeJS", "Go") val second = listOf("PHP", "Kotlin") val third = listOf("Ruby", "Perl") val result = concat(first, second, third) println(result) } |
Output:
[Ruby on Rails, NodeJS, Go, PHP, Kotlin, Ruby, Perl]
The code can be shortened using the forEach() function. Here’s the equivalent version of the above code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun <T> concat(vararg lists: List<T>): List<T> { val newList: MutableList<T> = mutableListOf() lists.forEach { newList.addAll(it) } return newList } fun main() { val first = listOf("Ruby on Rails", "NodeJS", "Go") val second = listOf("PHP", "Kotlin") val third = listOf("Ruby", "Perl") val result = concat(first, second, third) println(result) } |
Output:
[Ruby on Rails, NodeJS, Go, PHP, Kotlin, Ruby, Perl]
3. Using plus operator
A simple and fairly efficient solution to concatenate multiple lists is using the plus operator, which creates a new list that is the combination of all the lists.
|
1 2 3 4 5 6 7 8 |
fun main() { val first = listOf("Ruby on Rails", "NodeJS", "Go") val second = listOf("PHP", "Kotlin") val third = listOf("Ruby", "Perl") val result = first + second + third println(result) } |
Output:
[Ruby on Rails, NodeJS, Go, PHP, Kotlin, Ruby, Perl]
If we want to join only two equal-sized lists and pick up alternative elements from each list, the best option is to use the zip() function.
|
1 2 3 4 5 6 7 |
fun main() { val first = listOf("C", "C++") val second = listOf("Java", "Kotlin") val result = first.zip(second) { a, b -> listOf(a, b)}.flatten() println(result) } |
Output:
[C, Java, C++, Kotlin]
That’s all about joining multiple lists in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)