Kotlin Sequence count Function

The count function in Kotlin is used to count the number of elements in a sequence that satisfy a given predicate. It is part of the Kotlin standard library and allows you to determine the number of elements in a sequence that match a specific condition.

Table of Contents

  1. Introduction
  2. count Function Syntax
  3. Understanding count
  4. Examples
    • Basic Usage
    • Counting Elements in a Sequence of Strings
    • Using count with Custom Objects
    • Chaining count with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The count function allows you to count the number of elements in a sequence that satisfy a given predicate. This is useful for scenarios where you need to determine how many elements in a sequence match a specific condition, enabling efficient data analysis and manipulation.

count Function Syntax

The syntax for the count function is as follows:

inline fun <T> Sequence<T>.count(predicate: (T) -> Boolean): Int

Parameters:

  • predicate: A lambda function that defines the condition each element in the sequence must satisfy to be counted.

Returns:

  • The number of elements in the sequence that satisfy the given predicate.

Understanding count

The count function works by iterating through the sequence and applying the predicate to each element. It increments a counter each time an element satisfies the predicate and returns the final count.

Examples

Basic Usage

To demonstrate the basic usage of count, we will create a sequence of integers and count the number of even numbers.

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5, 6)
    val evenCount = numbers.count { it % 2 == 0 }
    println(evenCount) // Output: 3
}

Output:

3

Counting Elements in a Sequence of Strings

This example shows how to count the number of strings in a sequence with a length greater than 5.

Example

fun main() {
    val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha")
    val longNameCount = names.count { it.length > 5 }
    println(longNameCount) // Output: 3
}

Output:

3

Using count with Custom Objects

You can use the count function to count custom objects based on a specific condition.

Example

data class Person(val name: String, val age: Int)

fun main() {
    val people = sequenceOf(
        Person("Arjun", 25),
        Person("Bhaskar", 30),
        Person("Chitra", 22),
        Person("Deepak", 28),
        Person("Esha", 26)
    )
    val adultsCount = people.count { it.age >= 25 }
    println(adultsCount) // Output: 4
}

Output:

4

Chaining count with Other Functions

The count function can be chained with other sequence functions to perform more complex operations before counting the elements.

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val evenCount = numbers.filter { it > 5 }
                           .count { it % 2 == 0 }
    println(evenCount) // Output: 3
}

Output:

3

Real-World Use Case

Counting High-Priced Products

In real-world applications, the count function can be used to count products or other items based on a specific property, such as price.

Example

data class Product(val name: String, val price: Double)

fun main() {
    val products = sequenceOf(
        Product("Laptop", 999.99),
        Product("Smartphone", 499.99),
        Product("Tablet", 299.99),
        Product("Smartwatch", 199.99),
        Product("Headphones", 99.99)
    )

    val expensiveProductCount = products.count { it.price > 300 }
    println(expensiveProductCount) // Output: 2
}

Output:

2

Conclusion

The count function in Kotlin provides used for counting the number of elements in a sequence that satisfy a given predicate. By understanding and using the count function, you can efficiently manage and process data in your Kotlin applications, ensuring that you can determine how many elements match specific conditions according to your requirements.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top