Examples for collection transformations in Kotlin (#8912)

This commit is contained in:
Graham Cox 2020-03-26 04:31:54 +00:00 committed by GitHub
parent c5ba081f7c
commit 32ff2077a3
7 changed files with 267 additions and 0 deletions

View File

@ -0,0 +1,48 @@
package com.baeldung.collections.transformations
import org.junit.Assert.assertEquals
import org.junit.Test
class AssociateUnitTest {
@Test
fun testToMap() {
val input = listOf(Pair("one", 1), Pair("two", 2))
val map = input.toMap()
assertEquals(mapOf("one" to 1, "two" to 2), map)
}
@Test
fun testAssociateWith() {
val inputs = listOf("Hi", "there")
val map = inputs.associateWith { k -> k.length }
assertEquals(mapOf("Hi" to 2, "there" to 5), map)
}
@Test
fun testAssociateBy() {
val inputs = listOf("Hi", "there")
val map = inputs.associateBy { v -> v.length }
assertEquals(mapOf(2 to "Hi", 5 to "there"), map)
}
@Test
fun testAssociate() {
val inputs = listOf("Hi", "there")
val map = inputs.associate { e -> Pair(e.toUpperCase(), e.reversed()) }
assertEquals(mapOf("HI" to "iH", "THERE" to "ereht"), map)
}
@Test
fun testAssociateByDuplicateKeys() {
val inputs = listOf("one", "two")
val map = inputs.associateBy { v -> v.length }
assertEquals(mapOf(3 to "two"), map)
}
@Test
fun testGroupBy() {
val inputs = listOf("one", "two", "three")
val map = inputs.groupBy { v -> v.length }
assertEquals(mapOf(3 to listOf("one", "two"), 5 to listOf("three")), map)
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.collections.transformations
import org.junit.Assert.assertEquals
import org.junit.Test
class FilterUnitTest {
@Test
fun testFilterWithLambda() {
val input = listOf(1, 2, 3, 4, 5)
val filtered = input.filter { it <= 3 }
assertEquals(listOf(1, 2, 3), filtered)
}
@Test
fun testFilterWithMethodReference() {
val input = listOf(1, 2, 3, 4, 5)
val filtered = input.filter(this::isSmall)
assertEquals(listOf(1, 2, 3), filtered)
}
@Test
fun testFilterNotWithMethodReference() {
val input = listOf(1, 2, 3, 4, 5)
val filtered = input.filterNot(this::isSmall)
assertEquals(listOf(4, 5), filtered)
}
@Test
fun testFilterIndexed() {
val input = listOf(5, 4, 3, 2, 1)
val filtered = input.filterIndexed { index, element -> index < 3 }
assertEquals(listOf(5, 4, 3), filtered)
}
@Test
fun testFilterNotNull() {
val nullable: List<String?> = listOf("Hello", null, "World")
val nonnull: List<String> = nullable.filterNotNull()
assertEquals(listOf("Hello", "World"), nonnull)
}
private fun isSmall(i: Int) = i <= 3
}

View File

@ -0,0 +1,21 @@
package com.baeldung.collections.transformations
import org.junit.Assert.assertEquals
import org.junit.Test
class FlattenUnitTest {
@Test
fun testFlatten() {
val inputs = listOf("one", "two", "three")
val characters = inputs.map(String::toList)
val flattened = characters.flatten();
assertEquals(listOf('o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e'), flattened)
}
@Test
fun testFlatMap() {
val inputs = listOf("one", "two", "three")
val characters = inputs.flatMap(String::toList)
assertEquals(listOf('o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e'), characters)
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.collections.transformations
import org.junit.Assert.assertEquals
import org.junit.Test
class JoinToUnitTest {
@Test
fun testJoinToString() {
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
val simpleString = inputs.joinToString()
assertEquals("Jan, Feb, Mar, Apr, May", simpleString)
val detailedString = inputs.joinToString(separator = ",", prefix="Months: ", postfix=".")
assertEquals("Months: Jan,Feb,Mar,Apr,May.", detailedString)
}
@Test
fun testJoinToStringLimits() {
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
val simpleString = inputs.joinToString(limit = 3)
assertEquals("Jan, Feb, Mar, ...", simpleString)
}
@Test
fun testJoinToStringTransform() {
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
val simpleString = inputs.joinToString(transform = String::toUpperCase)
assertEquals("JAN, FEB, MAR, APR, MAY", simpleString)
}
@Test
fun testJoinTo() {
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
val output = StringBuilder()
output.append("My ")
.append(inputs.size)
.append(" elements: ")
inputs.joinTo(output)
assertEquals("My 5 elements: Jan, Feb, Mar, Apr, May", output.toString())
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.collections.transformations
import org.junit.Assert.assertEquals
import org.junit.Test
class MapUnitTest {
@Test
fun testMapWithLambda() {
val input = listOf("one", "two", "three")
val reversed = input.map { it.reversed() }
assertEquals(listOf("eno", "owt", "eerht"), reversed)
val lengths = input.map { it.length }
assertEquals(listOf(3, 3, 5), lengths)
}
@Test
fun testMapIndexed() {
val input = listOf(3, 2, 1)
val result = input.mapIndexed { index, value -> index * value }
assertEquals(listOf(0, 2, 2), result)
}
@Test
fun testMapNotNull() {
val input = listOf(1, 2, 3, 4, 5)
val smallSquares = input.mapNotNull {
if (it <= 3) {
it * it
} else {
null
}
}
assertEquals(listOf(1, 4, 9), smallSquares)
}
@Test
fun mapMapKeys() {
val inputs = mapOf("one" to 1, "two" to 2, "three" to 3)
val uppercases = inputs.mapKeys { it.key.toUpperCase() }
assertEquals(mapOf("ONE" to 1, "TWO" to 2, "THREE" to 3), uppercases)
}
@Test
fun mapMapValues() {
val inputs = mapOf("one" to 1, "two" to 2, "three" to 3)
val squares = inputs.mapValues { it.value * it.value }
assertEquals(mapOf("one" to 1, "two" to 4, "three" to 9), squares)
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.collections.transformations
import org.junit.Assert.assertEquals
import org.junit.Test
class ReduceUnitTest {
@Test
fun testJoinToStringAsReduce() {
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
val result = inputs.reduce { acc, next -> "$acc, $next" }
assertEquals("Jan, Feb, Mar, Apr, May", result)
}
@Test
fun testFoldToLength() {
val inputs = listOf("Jan", "Feb", "Mar", "Apr", "May")
val result = inputs.fold(0) { acc, next -> acc + next.length }
assertEquals(15, result)
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.collections.transformations
import org.junit.Assert.assertEquals
import org.junit.Test
class ZipUnitTest {
@Test
fun testZip() {
val left = listOf("one", "two", "three")
val right = listOf(1, 2, 3)
val zipped = left.zip(right)
assertEquals (listOf(Pair("one", 1), Pair("two", 2), Pair("three", 3)), zipped)
}
@Test
fun testZipShort() {
val left = listOf("one", "two")
val right = listOf(1, 2, 3)
val zipped = left.zip(right)
assertEquals (listOf(Pair("one", 1), Pair("two", 2)), zipped)
}
@Test
fun testUnzip() {
val left = listOf("one", "two", "three")
val right = listOf(1, 2, 3)
val zipped = left.zip(right)
val (newLeft, newRight) = zipped.unzip()
assertEquals(left, newLeft)
assertEquals(right, newRight)
}
}