From dc9a78ab18018f55764b4e64ca26c706211876c5 Mon Sep 17 00:00:00 2001 From: David Calap Date: Tue, 1 Oct 2019 23:33:50 +0200 Subject: [PATCH] BAEL-3323 Finding an element in a list using Kotlin - Initial commit --- .../kotlin/com/baeldung/lists/ListsTest.kt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt diff --git a/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt b/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt new file mode 100644 index 0000000000..8515a6f078 --- /dev/null +++ b/core-kotlin-2/src/test/kotlin/com/baeldung/lists/ListsTest.kt @@ -0,0 +1,25 @@ +package com.baeldung.lambda + +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class ListsTest { + + var batmans: List = listOf("Christian Bale", "Michael Keaton", "Ben Affleck", "George Clooney") + + @Test + fun whenFindASpecificItem_thenItemIsReturned() { + //Returns the first element matching the given predicate, or null if no such element was found. + val theFirstBatman = batmans.find { actor -> "Michael Keaton".equals(actor) } + assertEquals(theFirstBatman, "Michael Keaton") + } + + @Test + fun whenFilterWithPredicate_thenMatchingItemsAreReturned() { + //Returns a list containing only elements matching the given predicate. + val theCoolestBatmans = batmans.filter { actor -> actor.contains("a") } + assertTrue(theCoolestBatmans.contains("Christian Bale") && theCoolestBatmans.contains("Michael Keaton")) + } + +} \ No newline at end of file