Skip to content

Commit 6d91c3e

Browse files
authored
Merge pull request #777 from albertache1998/list-val
[KTLN-810] Change a Value in Mutable List in Kotlin
2 parents 1c69b37 + 563cd67 commit 6d91c3e

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package changeMutableListValue
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
6+
class ChangeMutableListValueUnitTest {
7+
8+
@Test
9+
fun `changes mutable list value using set method`() {
10+
val mutableList = mutableListOf("kotlin", "java", "android")
11+
mutableList.set(1, "swift")
12+
assertEquals(mutableListOf("kotlin", "swift", "android"), mutableList)
13+
}
14+
15+
@Test
16+
fun `changes mutable list value using indexed access operator`() {
17+
val mutableList = mutableListOf("kotlin", "java", "android")
18+
mutableList[1] = "swift"
19+
assertEquals(mutableListOf("kotlin", "swift", "android"), mutableList)
20+
}
21+
22+
@Test
23+
fun `creates new list with updated value using the map method`() {
24+
val mutableList = mutableListOf("kotlin", "java", "android")
25+
val updatedList = mutableList.map { element ->
26+
if (element == "java") {
27+
"swift"
28+
} else {
29+
element
30+
}
31+
}
32+
assertEquals(mutableListOf("kotlin", "swift", "android"), updatedList)
33+
}
34+
35+
@Test
36+
fun `changes mutable list value using the replace method`() {
37+
val mutableList = mutableListOf("kotlin", "java", "android")
38+
mutableList.replaceAll { if (it == "java") "swift" else it }
39+
assertEquals(mutableListOf("kotlin", "swift", "android"), mutableList)
40+
}
41+
}

0 commit comments

Comments
 (0)