diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt index 6371b1da96..e3477931bb 100644 --- a/kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt @@ -8,6 +8,7 @@ class ListToMapTest { val user1 = User("John", 18, listOf("Hiking, Swimming")) val user2 = User("Sara", 25, listOf("Chess, Board Games")) val user3 = User("Dave", 34, listOf("Games, Racing sports")) + val user4 = User("John", 30, listOf("Reading, Poker")) @Test fun givenList_whenConvertToMap_thenResult() { @@ -32,4 +33,42 @@ class ListToMapTest { assertTrue(myMap.get("a") == "a") } + + @Test + fun givenStringList_whenAssociate_thenResult() { + val myList = listOf("a", "b", "c", "c", "b") + val myMap = myList.associate{ it to it } + + assertTrue(myMap.get("a") == "a") + } + + @Test + fun givenStringList_whenAssociateTo_thenResult() { + val myList = listOf("a", "b", "c", "c", "b") + val myMap = mutableMapOf() + + myList.associateTo(myMap) {it to it} + + assertTrue(myMap.get("a") == "a") + } + + @Test + fun givenStringList_whenAssociateByTo_thenResult() { + val myList = listOf(user1, user2, user3, user4) + val myMap = mutableMapOf() + + myList.associateByTo(myMap, {it.name}, {it.age}) + + assertTrue(myMap.get("Dave") == 34) + } + + @Test + fun givenStringList_whenAssociateByToUser_thenResult() { + val myList = listOf(user1, user2, user3, user4) + val myMap = mutableMapOf() + + myList.associateByTo(myMap) {it.name} + + assertTrue(myMap.get("Dave")!!.age == 34) + } } \ No newline at end of file