Join two lists in Kotlin
This article explores different ways to join two lists in Kotlin.
1. Using Plus Operator
A simple solution to join two lists is with the plus operator. To illustrate, the following creates a new list, which is the concatenation of both lists.
1 2 3 4 5 6 7 8 9 10 11 12 |
fun <T> merge(first: List<T>, second: List<T>): List<T> { return first + second } fun main() { val clike = listOf("C", "C++") val javalike = listOf("Java", "Kotlin") var list: List<String>? = merge(clike, javalike) println(list) // [C, C++, Java, Kotlin] } |
An equivalent way is to use the plus()
function.
1 2 3 4 5 6 7 8 9 10 11 12 |
fun <T> merge(first: List<T>, second: List<T>): List<T> { return first.plus(second) } fun main() { val clike = listOf("C", "C++") val javalike = listOf("Java", "Kotlin") var list: List<String>? = merge(clike, javalike) println(list) // [C, C++, Java, Kotlin] } |
2. Using addAll()
function
Alternatively, you can use the native addAll()
function, which appends all specified collection elements at the end of the list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
fun <T> merge(first: List<T>, second: List<T>): List<T> { val list: MutableList<T> = ArrayList() list.addAll(first) list.addAll(second) return list } fun main() { val clike = listOf("C", "C++") val javalike = listOf("Java", "Kotlin") var list: List<String> = merge(clike, javalike) println(list) // [C, C++, Java, Kotlin] } |
You can also avoid the first call to the addAll()
function by initializing the list with the first list using the ArrayList
constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
fun <T> merge(first: List<T>, second: List<T>): List<T> { val list: MutableList<T> = ArrayList(first) list.addAll(second) return list } fun main() { val clike = listOf("C", "C++") val javalike = listOf("Java", "Kotlin") var list: List<String> = merge(clike, javalike) println(list) // [C, C++, Java, Kotlin] } |
3. Using Double Brace Initialization
You can also use Double Brace Initialization, which internally creates an anonymous inner class with an instance initializer in it. We should avoid this approach.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
fun <T> merge(first: List<T>, second: List<T>): List<T> { return object : ArrayList<T>() { init { addAll(first) addAll(second) } } } fun main() { val clike = listOf("C", "C++") val javalike = listOf("Java", "Kotlin") var list: List<String> = merge(clike, javalike) println(list) // [C, C++, Java, Kotlin] } |
4. Using Java 8 Stream
You can also use Stream to join two lists, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.stream.Stream fun <T> merge(first: List<T>, second: List<T>): List<T> { val list: MutableList<T> = ArrayList() Stream.of(first, second).forEach { item: List<T>? -> list.addAll(item!!) } return list } fun main() { val clike = listOf("C", "C++") val javalike = listOf("Java", "Kotlin") var list: List<String>? = merge(clike, javalike) println(list) // [C, C++, Java, Kotlin] } |
That’s all about joining two 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 :)