Skip to content

Commit bf44688

Browse files
[KTLN-480] Convert a Collection Into an ArrayList in Kotlin (#813)
* added unit test code * refactored code * refactored code * Extra Spacing Cleanup --------- Co-authored-by: Brandon Ward <[email protected]>
1 parent 1182a7f commit bf44688

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-kotlin-collections-6</artifactId>
7+
<name>core-kotlin-collections-6</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>core-kotlin-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.collectionToArrayList
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import kotlin.collections.ArrayList
6+
7+
class CollectionToArrayListUnitTest {
8+
9+
@Test
10+
fun `converts a Collection to ArrayList using the for loop and add method`() {
11+
val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
12+
val arrayList = ArrayList<String>()
13+
for (element in collection) {
14+
arrayList.add(element)
15+
}
16+
17+
assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
18+
}
19+
20+
@Test
21+
fun `converts a Collection to ArrayList using the ArrayList constructor`() {
22+
val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
23+
val arrayList = ArrayList(collection)
24+
25+
assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
26+
}
27+
28+
@Test
29+
fun `converts a Collection to ArrayList using the toCollection method`() {
30+
val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
31+
val arrayList = collection.toCollection(ArrayList())
32+
33+
assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
34+
}
35+
36+
@Test
37+
fun `converts a Collection to ArrayList using the addAll method`() {
38+
val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
39+
val arrayList = ArrayList<String>()
40+
arrayList.addAll(collection)
41+
42+
assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
43+
}
44+
45+
@Test
46+
fun `converts a Collection to ArrayList using the mapTo method`() {
47+
val collection: Collection<String> = listOf("Kotlin", "Java", "Scala")
48+
val arrayList = ArrayList<String>()
49+
collection.mapTo(arrayList) { it }
50+
51+
assertEquals(arrayListOf("Kotlin", "Java", "Scala"), arrayList)
52+
}
53+
}

core-kotlin-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<module>core-kotlin-collections-3</module>
3737
<module>core-kotlin-collections-4</module>
3838
<module>core-kotlin-collections-5</module>
39+
<module>core-kotlin-collections-6</module>
3940
<module>core-kotlin-collections-list</module>
4041
<module>core-kotlin-collections-list-2</module>
4142
<module>core-kotlin-collections-map</module>

0 commit comments

Comments
 (0)