Kotlin List Count Example

The count() method returns the number of elements in the list.

Kotlin List Count Example

The example returns the number of values in the list, the number of negative values and the number of even values.
package net.sourcecodeexamples

fun main() {

    val nums = listOf(12, 1,2,5,4,2)

    val len = nums.count()
    println("There are $len elements")
    val size = nums.size
    println("The size of the list is $size")    

    val n1 = nums.count { e -> e < 0 }
    println("There are $n1 negative values")

    val n2 = nums.count { e -> e % 2 == 0 }
    println("There are $n2 even values")
There are 6 elements
The size of the list is 6
There are 0 negative values
There are 4 even values

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course