Skip to content

Commit e353d6d

Browse files
authored
Merge pull request eugenp#7926 from dcalap/master
BAEL-3323 Finding an element in a list using Kotlin - Initial commit
2 parents 25023df + 0478d46 commit e353d6d

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.lists
2+
3+
import org.junit.jupiter.api.Test
4+
import kotlin.test.assertEquals
5+
import kotlin.test.assertFalse
6+
import kotlin.test.assertTrue
7+
8+
class ListsUnitTest {
9+
10+
var batmans: List<String> = listOf("Christian Bale", "Michael Keaton", "Ben Affleck", "George Clooney")
11+
12+
@Test
13+
fun whenFindASpecificItem_thenItemIsReturned() {
14+
//Returns the first element matching the given predicate, or null if no such element was found.
15+
val theFirstBatman = batmans.find { actor -> "Michael Keaton".equals(actor) }
16+
assertEquals(theFirstBatman, "Michael Keaton")
17+
}
18+
19+
@Test
20+
fun whenFilterWithPredicate_thenMatchingItemsAreReturned() {
21+
//Returns a list containing only elements matching the given predicate.
22+
val theCoolestBatmans = batmans.filter { actor -> actor.contains("a") }
23+
assertTrue(theCoolestBatmans.contains("Christian Bale") && theCoolestBatmans.contains("Michael Keaton"))
24+
}
25+
26+
@Test
27+
fun whenFilterNotWithPredicate_thenMatchingItemsAreReturned() {
28+
//Returns a list containing only elements not matching the given predicate.
29+
val theMehBatmans = batmans.filterNot { actor -> actor.contains("a") }
30+
assertFalse(theMehBatmans.contains("Christian Bale") && theMehBatmans.contains("Michael Keaton"))
31+
assertTrue(theMehBatmans.contains("Ben Affleck") && theMehBatmans.contains("George Clooney"))
32+
}
33+
34+
}

0 commit comments

Comments
 (0)