Skip to content

Commit 63d87fa

Browse files
yadavan88fabiotadashi
authored andcommitted
Added more methods
1 parent e60b34f commit 63d87fa

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

core-kotlin-modules/core-kotlin-collections-set/src/test/kotlin/com/baeldung/arraytoset/ArrayToSetUnitTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class ArrayToSetUnitTest {
2121
@Test
2222
fun `convert array to set using HashSet`() {
2323
val arr = arrayOf(1, 5, 2, 4, 1)
24-
val set = HashSet(arr.toList())
24+
val set = LinkedHashSet(arr.toList())
25+
2526
assertEquals(setOf(1, 5, 2, 4), set)
2627
}
2728

core-kotlin-modules/core-kotlin-collections-set/src/test/kotlin/com/baeldung/settoarray/SetToArrayUnitTest.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,18 @@ class SetToArrayUnitTest {
1818
val arr = Array(set.size) { set.elementAt(it) }
1919
Assertions.assertArrayEquals(arrayOf(1, 2, 3, 4), arr)
2020
}
21+
22+
@Test
23+
fun `convert set to array using loop`() {
24+
val set = setOf(1, 2, 3, 4)
25+
val arr = arrayOfNulls<Int>(set.size)
26+
//val arr = Array(set.size) { 0 }
27+
28+
var index = 0
29+
for (element in set) {
30+
arr[index++] = element
31+
}
32+
33+
Assertions.assertArrayEquals(arrayOf(1, 2, 3, 4), arr)
34+
}
2135
}

0 commit comments

Comments
 (0)