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
+ }
0 commit comments