VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/merge-two-collections-in-kotlin/

⇱ Merge Two Collections in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Merge Two Collections in Kotlin

Last Updated : 15 Jun, 2025

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.

  1. 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.
  2. 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:


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:

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:

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:

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:

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:

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:

Output:

[a, b, c, d, e, f]

This method does exactly what the + operator does. It combines two lists into a new one.

Comment
Article Tags:

Explore