Join Two Lists in Kotlin
A quick programming guide to join two lists in kotlin. Joining two lists or ArrayList can be done using List addAll() or apache commons ListUtils.union() method and show examples on each method.
1. Introduction
In this article, You’ll learn how to join two lists in kotlin programming language. List api has addAll() method and apache ListUtils.union() method to add two lists in kotlin.
First, we’ll see the example program with built-in api method addAll() and next further using ListUtils.union() method from apache commons api.
2. Example to Join Two Lists in Kotlin – ArrayList
First Example program to add two ArrayList’s in kotlin and both lists stores the String type values.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.javaprogramto.kotlin.collections.list fun main() { var list1 = ArrayList<String>(); list1.add( "one" ) list1.add( "two" ) var list2 = ArrayList<String>() list2.add( "three" ) list2.add( "four" ) var joinedList = ArrayList<String>() joinedList.addAll(list1); joinedList.addAll(list2) println( "List one : $list1" ) println( "List two : $list2" ) println( "After adding two lists in kotlin program is : $joinedList" ) } |
Output:
List one : [one, two]
List two : [three, four]
After adding two lists in kotlin program is : [one, two, three, four
The above program used List.addAll() method to add two lists list1 and list2. Finally, all values of list1 and list2 are added to the joinedList.
addAll() method can take any number of list objects and size will grown as needed.
Additionally, you can add list2 from index 1 in joinedList as below. That means, Inserts all of the elements in the specified collection into a joined list, starting at the specified position.
See the output for a better understanding.
1 2 3 4 5 6 | joinedList.addAll(list1); joinedList.addAll( 1 , list2) println( "List one : $list1" ) println( "List two : $list2" ) println( "After adding two lists in kotlin program is : $joinedList" ) |
Output:
List one : [one, two]
List two : [three, four]
After adding two lists in the kotlin program is : [one, three, four, two
3. Example to Add Two Integer Lists in Kotlin – LinkedList
Next, Example program to add two LinkedList’s in kotlin and both take Integer values.
Kotlin does not support autoboxing and unboxing as in java. So, you must specify the exact type of the data. Here, you should use the
kotlin.Int type for List.
So, you must create the objects with the specified wrapper class.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.javaprogramto.kotlin.list.join import java.util.* import kotlin.collections.ArrayList fun main() { var list1 = LinkedList<Int>(); list1.add( 100 ) list1.add( 200 ) var list2 = LinkedList<Int>(); list2.add( 300 ) list2.add( 400 ) var joinedList = LinkedList<Int>() joinedList.addAll(list1); joinedList.addAll(list2) println( "LinkedList one : $list1" ) println( "LinkedList two : $list2" ) println( "After adding two LinkedList in kotlin program is : $joinedList" ) } |
Output:
LinkedList one : [100, 200]
LinkedList two : [300, 400]
After adding two LinkedList in kotlin program is : [100, 200, 300, 400
4. Example to Merge Two Lists using union() method
Last example program to merge two lists using union() in kotlin.
First of all, before using union() method, you must add apache commons maven dependency as below.
1 2 3 4 5 | <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version> 4.1 </version> </dependency> |
Example:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.javaprogramto.kotlin.list import org.apache.commons.collections4.ListUtils fun main() { var list1 = ArrayList<String>(); list1.add( "hello" ) list1.add( "world" ) var list2 = ArrayList<String>() list2.add( "welcome" ) list2.add( "java developer" ) var joinedNewList = ListUtils.union(list1, list2); println( "List one : $list1" ) println( "List two : $list2" ) println( "After adding two lists using ListUtils.union() method is : $joinedNewList" ) } |
Output:
List one : [hello, world]
List two : [welcome, java developer]
After adding two lists using ListUtils.union() method is : [hello, world, welcome, java developer
In the above program, the Invoked union() method to add two lists and returns new ArrayList with all two list values.
Even, if you pass two LinkedList lists to union() method still it returns a new ArrayList object.
5. Conclusion
In conclusion, You’ve seen how to merge two lists into a single list using
List.addAll() and apache commons ListUtils.union().
All of these examples produce the same output. You have to choose the right one. If you want to merge the values as LinkedList then go for addAll() because union() method always returns ArrayList.
All the code is shown in this article is over GitHub.
You can download the project directly and can run in your local without any errors.
If you have any queries please post in the comment section.
Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Join Two Lists in Kotlin Opinions expressed by Java Code Geeks contributors are their own. |
You can join two lists i Kotlin by using +.
Also simply:
Ah … comment below already mentioned.