From d6d5386613187ddfd692c777b94e70f96e70a83b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walter=20G=C3=B3mez?= Date: Thu, 6 Jul 2017 01:27:32 -0600 Subject: [PATCH] Feature/kotlin list to map (#2216) * 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 --- .../com/baeldung/kotlin/ListToMapTest.kt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt new file mode 100644 index 0000000000..6371b1da96 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/ListToMapTest.kt @@ -0,0 +1,35 @@ +package com.baeldung.kotlin + +import org.junit.Test +import kotlin.test.assertTrue + +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")) + + @Test + fun givenList_whenConvertToMap_thenResult() { + val myList = listOf(user1, user2, user3) + val myMap = myList.map { it.name to it.age }.toMap() + + assertTrue(myMap.get("John") == 18) + } + + @Test + fun givenList_whenAssociatedBy_thenResult() { + val myList = listOf(user1, user2, user3) + val myMap = myList.associateBy({ it.name }, { it.hobbies }) + + assertTrue(myMap.get("John")!!.contains("Hiking, Swimming")) + } + + @Test + fun givenStringList_whenConvertToMap_thenResult() { + val myList = listOf("a", "b", "c") + val myMap = myList.map { it to it }.toMap() + + assertTrue(myMap.get("a") == "a") + } +} \ No newline at end of file