[BAEL-19886] - moved related articles to the new core-kotlin-modules/core-kotlin module
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom
|
||||
|
||||
class ListExtension {
|
||||
fun <T> List<T>.random(): T? {
|
||||
if (this.isEmpty()) return null
|
||||
return get(ThreadLocalRandom.current().nextInt(count()))
|
||||
}
|
||||
|
||||
fun <T> getRandomElementOfList(list: List<T>): T? {
|
||||
return list.random()
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.baeldung.kotlin
|
||||
|
||||
import com.baeldung.kotlin.ListExtension
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class ListExtensionTest {
|
||||
@Test
|
||||
fun givenList_whenExecuteExtensionFunctionOnList_shouldReturnRandomElementOfList(){
|
||||
//given
|
||||
val elements = listOf("a", "b", "c")
|
||||
|
||||
//when
|
||||
val result = ListExtension().getRandomElementOfList(elements)
|
||||
|
||||
//then
|
||||
assertTrue(elements.contains(result))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user