From 32ff2077a37ab1bea794aaca9eb9a3f566e055c9 Mon Sep 17 00:00:00 2001 From: Graham Cox Date: Thu, 26 Mar 2020 04:31:54 +0000 Subject: [PATCH] Examples for collection transformations in Kotlin (#8912) --- .../transformations/AssociateUnitTest.kt | 48 +++++++++++++++++ .../transformations/FilterUnitTest.kt | 43 +++++++++++++++ .../transformations/FlattenUnitTest.kt | 21 ++++++++ .../transformations/JoinToUnitTest.kt | 46 ++++++++++++++++ .../transformations/MapUnitTest.kt | 53 +++++++++++++++++++ .../transformations/ReduceUnitTest.kt | 22 ++++++++ .../transformations/ZipUnitTest.kt | 34 ++++++++++++ 7 files changed, 267 insertions(+) create mode 100644 core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/AssociateUnitTest.kt create mode 100644 core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/FilterUnitTest.kt create mode 100644 core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/FlattenUnitTest.kt create mode 100644 core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/JoinToUnitTest.kt create mode 100644 core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/MapUnitTest.kt create mode 100644 core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/ReduceUnitTest.kt create mode 100644 core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/ZipUnitTest.kt diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/AssociateUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/AssociateUnitTest.kt new file mode 100644 index 0000000000..68f7040c4c --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/AssociateUnitTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/FilterUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/FilterUnitTest.kt new file mode 100644 index 0000000000..591577e4f3 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/FilterUnitTest.kt @@ -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 = listOf("Hello", null, "World") + val nonnull: List = nullable.filterNotNull() + assertEquals(listOf("Hello", "World"), nonnull) + } + + private fun isSmall(i: Int) = i <= 3 +} \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/FlattenUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/FlattenUnitTest.kt new file mode 100644 index 0000000000..69fbceb8e3 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/FlattenUnitTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/JoinToUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/JoinToUnitTest.kt new file mode 100644 index 0000000000..2ac0cdca50 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/JoinToUnitTest.kt @@ -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()) + } +} \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/MapUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/MapUnitTest.kt new file mode 100644 index 0000000000..e22fcbe903 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/MapUnitTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/ReduceUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/ReduceUnitTest.kt new file mode 100644 index 0000000000..6821b7cdb9 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/ReduceUnitTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/ZipUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/ZipUnitTest.kt new file mode 100644 index 0000000000..66aeeceef4 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/transformations/ZipUnitTest.kt @@ -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) + } + +} \ No newline at end of file