3 ways to concatenate two collections with distinct elements in Kotlin

Concatenate two collections with distinct elements in Kotlin :

In this tutorial, we will learn how to concatenate two collections with distinct elements. We will use two lists to show you the example. If the first list contains elements (1,2,3,4) and the second list contains (5,6,7), the final list after concatenation will hold (1,2,3,4,5,6,7).

We will show you three different ways to solve this problem :

  1. Using addAll().
  2. Using union().
  3. Using ’+’ operator.

Using addAll() :

addAll() is a simple function to add elements of one collection to the end of a different collection. It is defined as below :

public boolean addAll(Collection<? extends E> c)

It appends all the elements in the collection to the end of the list. In this example, we will use ArrayList of integers.

  • It takes one parameter: the collection holding the elements to be added to this list.
  • It returns true on success and false otherwise.

Example :

fun main() {
    val firstList: ArrayList<Int> = arrayListOf(1, 2, 3, 4)
    val secondList: ArrayList<Int> = arrayListOf(5, 6, 7, 8)

    val finalList: ArrayList<Int> = ArrayList()

    println("First list $firstList")
    println("Second list $secondList")

    println("Adding first list to the final list...")
    finalList.addAll(firstList)
    println("Final list : $finalList")

    println("Adding second list to the final list...")
    finalList.addAll(secondList)
    println("Final list : $finalList")
}

It will print the below output :

First list [1, 2, 3, 4]
Second list [5, 6, 7, 8]
Adding first list to the final list...
Final list : [1, 2, 3, 4]
Adding second list to the final list...
Final list : [1, 2, 3, 4, 5, 6, 7, 8]

As you can see that addAll() appends the content to the end of the list.

Using union() :

union() works differently than addAll. It will add only the unique elements of the second list to the first list. union() is defined as below :

public infix fun  Iterable.union(other: Iterable): Set

As you can see that it will return one set with all distinct elements of both lists. The order will be same. For example :

fun main() {
    val firstList: ArrayList<Int> = arrayListOf(1, 2, 3, 4)
    val secondList: ArrayList<Int> = arrayListOf(1, 5, 6, 7, 8)

    val finalList = firstList.union(secondList)

    println("First list : $firstList")
    println("Second list : $secondList")
    println("Final list : $finalList")
}

It will print :

First list : [1, 2, 3, 4]
Second list : [1, 5, 6, 7, 8]
Final list : [1, 2, 3, 4, 5, 6, 7, 8]

As you can see that even though the second list contains one common element 1 as the first list, it was removed in the final list.

Using the plus (+) operator :

Instead of using the above two methods, we can also use the plus (+) operator to add two lists. For example :

fun main() {
    val firstList: ArrayList<Int> = arrayListOf(1, 2, 3, 4)
    val secondList: ArrayList<Int> = arrayListOf(1, 5, 6, 7, 8)

    val finalList = firstList + secondList

    println("First list : $firstList")
    println("Second list : $secondList")
    println("Final list : $finalList")
}

Output :

First list : [1, 2, 3, 4]
Second list : [1, 5, 6, 7, 8]
Final list : [1, 2, 3, 4, 1, 5, 6, 7, 8]

As you can see, both lists are appended to the final list in this example.

Conclusion :

We learned three different ways to append two lists in kotlin. If you don’t want to include similar items in the final list, then you can use union(). Else, you can go for any of the first or third methods we have discussed. Try the examples above and drop one comment below if you have any queries.

You might also like :