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
This commit is contained in:
Walter Gómez 2017-07-06 01:27:32 -06:00 committed by Grzegorz Piwowarek
parent a57103de78
commit d6d5386613
1 changed files with 35 additions and 0 deletions

View File

@ -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")
}
}