Skip to content

Commit 2863d0c

Browse files
Walter Gómezpivovarit
authored andcommitted
Feature/kotlin map examples (eugenp#2220)
* Add project for hibernate immutable article Add Event entity Add hibernate configuration file Add hibernateutil for configuration Add test to match snippets from article * Update master * Add Kotlin test to convert list to map * Add additional associate methods on test for ListToMap file
1 parent 5a184d4 commit 2863d0c

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class ListToMapTest {
88
val user1 = User("John", 18, listOf("Hiking, Swimming"))
99
val user2 = User("Sara", 25, listOf("Chess, Board Games"))
1010
val user3 = User("Dave", 34, listOf("Games, Racing sports"))
11+
val user4 = User("John", 30, listOf("Reading, Poker"))
1112

1213
@Test
1314
fun givenList_whenConvertToMap_thenResult() {
@@ -32,4 +33,42 @@ class ListToMapTest {
3233

3334
assertTrue(myMap.get("a") == "a")
3435
}
36+
37+
@Test
38+
fun givenStringList_whenAssociate_thenResult() {
39+
val myList = listOf("a", "b", "c", "c", "b")
40+
val myMap = myList.associate{ it to it }
41+
42+
assertTrue(myMap.get("a") == "a")
43+
}
44+
45+
@Test
46+
fun givenStringList_whenAssociateTo_thenResult() {
47+
val myList = listOf("a", "b", "c", "c", "b")
48+
val myMap = mutableMapOf<String, String>()
49+
50+
myList.associateTo(myMap) {it to it}
51+
52+
assertTrue(myMap.get("a") == "a")
53+
}
54+
55+
@Test
56+
fun givenStringList_whenAssociateByTo_thenResult() {
57+
val myList = listOf(user1, user2, user3, user4)
58+
val myMap = mutableMapOf<String, Int>()
59+
60+
myList.associateByTo(myMap, {it.name}, {it.age})
61+
62+
assertTrue(myMap.get("Dave") == 34)
63+
}
64+
65+
@Test
66+
fun givenStringList_whenAssociateByToUser_thenResult() {
67+
val myList = listOf(user1, user2, user3, user4)
68+
val myMap = mutableMapOf<String, User>()
69+
70+
myList.associateByTo(myMap) {it.name}
71+
72+
assertTrue(myMap.get("Dave")!!.age == 34)
73+
}
3574
}

0 commit comments

Comments
 (0)