Open In App

Merge Two Collections in Kotlin

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a “better language” than Java, but still be fully interoperable with Java code. In this article, we will see how to merge two or more collections into one. However, before we move ahead, we need to understand the difference between mutable and immutable types. An immutable type of object is an object that cannot be changed. For example, if we define an immutable list, we won’t be able to add other objects to it. 

Example

You can create a list in Kotlin with the listof method. However, the list returned by this method is an immutable list, so we need to create a mutable list in order to add objects to it. Let’s check out the mentioned steps:

Step 1. Let’s create two lists, listA and listB, as follows

var listA= mutableListof<String> ("a","a","b")
var listB= mutableListOf<String> ("a", "c")

If the type declaration is inferred from the objects inside the listOf/mutableListof method, we won’t need to declare the type declaration explicitly. So, the preceding code will be rewritten as mutableListOf (“a”, “a”, “b”).

Step 2. Now, we will try to add the contents of listA in listB. For that purpose, we will require the addAll( ) method:

Kotlin




fun main (args: Array <String>) {
  val listA= mutableListof<String> ("a", "a", "b")
  val lists- mutableListof<String> ("a", "c")
  listB.addAll(listA)
  println(listB)
}


Output:

[a, c, a, a, b]

Step 3. Another way to merge two lists is using union. This returns the unique elements of the combined collection

Kotlin




fun main (args: Array<String>) {
  val listA = mutableListof<String>("a","a","b")
  val listB = mutableListOf<String>("a","c")
  val listC = listB.union(listA)
  println(listC)
}


Output:

[a, c, b]

Step 4. Similarly, mutable sets can be merged too, the only difference is that addAll in a set will be similar to what we will receive with the union method; since it’s a set, only a unique value is allowed:

Kotlin




val setA= mutableSetOf<String> ("a", "b","c")
val setB= mutableSetOf<String>("a", "b","c","d")
setB.addAll(setA)
println(setB)
println (sets.union(setA))


Output:

[a, b, c, d]
[a, b, c, d]

If you want to merge two maps, you will need the putAll ( ) method, as addAll and union are not present for the map:

Kotlin




val mapA= mutableMapof <String, Int> ("a" to 1, "b" to 2)
val mapB= mutableMapOf<String, Int> ("a" to 2, "d" to 4)
mapA.putAll(mapB)
println(mapA)


Output:

(a=2, b=2, d=4}

Notice that key a was defined in both the maps, but the one that comes later (in this case, mapB) wins.

In Kotlin, you can merge two collections using the plus operator or the plus function.

The plus operator allows you to combine two collections into a new collection. Here is an example:

Kotlin




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]


Output:

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

In this example, we have two lists list1 and list2, and we want to merge them into a single list. We use the + operator to concatenate the two lists, and the resulting list is stored in the result variable. We then print out the contents of result, which is [a, b, c, d, e, f].

Alternatively, you can use the plus function to achieve the same result. Here is an example:

Kotlin




val list1 = listOf("a", "b", "c")
val list2 = listOf("d", "e", "f")
 
val result = list1.plus(list2)
 
println(result) //


Output:

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

In this example, we call the plus function on list1 and pass in list2 as a parameter. The resulting list is stored in the result variable, and we print out the contents of result, which is [a, b, c, d, e, f].



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads