Concatenate multiple lists in Kotlin
In this article, several methods to concatenate multiple lists into a single list in Kotlin.
1. Using flatten() function
A simple solution is to use the flatten() function, which returns a single list of all elements from the given sequence. The following example demonstrates its usage with the help of the Spread operator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun <T> concatenate(vararg lists: List<T>): List<T> { return listOf(*lists).flatten() } fun main() { val first = listOf('A', 'B') val second = listOf('C', 'D') val third = listOf('E', 'F') val list = concatenate(first, second, third) println(list) // [A, B, C, D, E, F] } |
2. Using Plus Operator
Another simple and fairly efficient solution to combine two or more lists is with the plus operator. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun <T> concatenate(vararg lists: List<T>): List<T> { val result: MutableList<T> = ArrayList() for (list in lists) { result += list } return result } fun main() { val first = listOf('A', 'B') val second = listOf('C', 'D') val third = listOf('E', 'F') val list = concatenate(first, second, third) println(list) // [A, B, C, D, E, F] } |
3. Using addAll() function
Alternatively, you can use the addAll() function that adds specified elements to the specified collection.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
fun <T> concatenate(vararg lists: List<T>): List<T> { val result: MutableList<T> = ArrayList() for (list in lists) { result.addAll(list) } return result } fun main() { val first = listOf('A', 'B') val second = listOf('C', 'D') val third = listOf('E', 'F') val list = concatenate(first, second, third) println(list) // [A, B, C, D, E, F] } |
You can also accumulate all elements using forEach(), as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun <T> concatenate(vararg lists: List<T>): List<T> { val result: MutableList<T> = ArrayList() lists.forEach { list: List<T> -> result.addAll(list) } return result } fun main() { val first = listOf('A', 'B') val second = listOf('C', 'D') val third = listOf('E', 'F') val list = concatenate(first, second, third) println(list) // [A, B, C, D, E, F] } |
4. Using Double Brace Initialization
Although not recommended, the Double Brace Initialization technique can be used to concatenate multiple lists into a single list. It creates an anonymous inner class with an instance initializer in it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
fun <T> concatenate(vararg lists: List<T>): MutableList<T> { return object : ArrayList<T>() { init { for (list in lists) { addAll(list) } } } } fun main() { val first = listOf('A', 'B') val second = listOf('C', 'D') val third = listOf('E', 'F') val list = concatenate(first, second, third) println(list) // [A, B, C, D, E, F] } |
That’s all about concatenating 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 :)