0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Kotlin Collection

Posted at
import org.junit.Test
import org.junit.Assert.*
import kotlin.math.pow
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        val nums = listOf(10, 20, 100, 5)
       //1. check true of false
        assert(nums.any()) // true if nums is not empty
        assert(nums.any{it > 99})
        assertFalse(nums.all { it > 99 })
        assert(nums.none{it > 101})
        // 2. filter
        assertEquals(listOf(10,5), nums.filter { it <= 10 })
        assertEquals(listOf(20,100), nums.filterNot { it <= 10 })
        // 3. map
        val ints : List<Double?> = listOf(2.0,null, 3.0)
        assertEquals(listOf(4.0, null, 9.0), ints.map { it?.pow(2) })
        assertEquals(listOf(4.0, 9.0), ints.mapNotNull { it?.pow(2) })
        //4. group
        assertEquals(mapOf("odd" to listOf(1,3), "even" to listOf(2,4)),
            listOf(1,2,3,4).groupBy { when {it % 2 == 1 ->"odd" else -> "even"} })
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?