Kotlin is a statically typed, general-purpose programming language developed by JetBrains. The company famous for creating world-class IDEs like IntelliJ IDEA, PhpStorm, and AppCode. Kotlin was first introduced by JetBrains in 2011 and is designed to run on the Java Virtual Machine (JVM). It is an object-oriented language and is often seen as a âbetter languageâ compared to Java because of its concise syntax and safety features. Still, Kotlin is fully interoperable with Java, which means we can use Kotlin and Java code together in the same project without any issues.
In this article, we will learn how to merge two or more collections into one in Kotlin.
Mutable vs Immutable Collections
In Kotlin, a collection (like a list or a set) can be either mutable or immutable.
- Immutable Collection: Once this collection is created, we cannot change it. For example, if we create an immutable list, we cannot add, remove, or modify any element in it.
- Mutable Collection: This collection allows changes. We can add, remove, or modify elements freely.
Example:
val list = listOf("a", "b", "c") // Immutable list
val mutableList = mutableListOf("a", "b", "c") // Mutable list
In the first case, we cannot add or remove items from list. But with mutableList, we can change its content.
How to Merge Two Lists
Step 1: Create Two Lists
Let's create two lists, listA and listB, as shown below.
Example:
var listA = mutableListOf("a", "a", "b")
var listB = mutableListOf("a", "c")
Notice that Kotlin can understand (infer) the type of these lists from the values we put inside them, so we donât need to specify <String> unless we really want to.
Step 2: Merge Lists Using addAll() Method
To merge the contents of listA into listB, we can use the addAll() method.
Example:
listB.addAll(listA)
println(listB)
Output:
[a, c, a, a, b]In this case, all elements from listA are added to listB. Notice that duplicates are not removed, both lists are simply combined.
Step 3: Merge Lists Using union() Function
If we want to merge two lists but keep only unique elements, we can use the union() function.
Example:
val result = listA.union(listB)
println(result)
Output:
[a, c, b]Here, union() returns a new set containing only unique values from both lists.
Step 4: Merge Two Sets
The same concept works with sets. Sets automatically store only unique values.
Example:
val setA = mutableSetOf("a", "b", "c")
val setB = mutableSetOf("b", "c", "d")
setA.addAll(setB)
println(setA)
Output:
[a, b, c, d]With sets, addAll() and union() give the same result, because sets never allow duplicate values.
Step 5: Merge Two Maps
For maps (key-value pairs), we cannot use addAll() or union(). Instead, we use putAll().
Example:
val mapA = mutableMapOf("a" to 1, "b" to 2)
val mapB = mutableMapOf("a" to 2, "d" to 4)
mapA.putAll(mapB)
println(mapA)
Output:
{a=2, b=2, d=4}Notice that the key "a" was present in both maps. After merging, the value from mapB replaces the value from mapA, because putAll() overwrites existing keys.
Step 6: Merge Using the Plus (+) Operator
Another easy way to merge two collections is by using the + operator.
Example:
val list1 = listOf("a", "b", "c")
val list2 = listOf("d", "e", "f")
val result = list1 + list2
println(result)
Output:
[a, b, c, d, e, f]The + operator joins both lists and returns a new list. It does not change the original lists.
Step 7: Merge Using the plus() Function
We can also use the plus() function in the same way.
Example:
val result = list1.plus(list2)
println(result)
Output:
[a, b, c, d, e, f]This method does exactly what the + operator does. It combines two lists into a new one.