Kotlin filterTo Function

The filterTo function in Kotlin is used to filter elements of a collection based on a given predicate and store the filtered elements in a specified destination collection. This function belongs to the Kotlin standard library and provides a flexible way to filter elements and collect the results in a custom destination.

Table of Contents

  1. Introduction
  2. filterTo Function Syntax
  3. Understanding filterTo
  4. Examples
    • Basic Usage
    • Filtering to a Mutable List
    • Filtering to a Mutable Set
  5. Real-World Use Case
  6. Conclusion

Introduction

The filterTo function allows you to filter elements of a collection (such as a list or set) based on a given predicate and store the resulting elements in a specified destination collection. This is useful for scenarios where you need to collect the filtered results in a custom collection.

filterTo Function Syntax

The syntax for the filterTo function is as follows:

fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(destination: C, predicate: (T) -> Boolean): C 

Parameters:

  • destination: The collection where the filtered elements will be stored.
  • predicate: A lambda function that takes an element of the collection as input and returns true if the element should be included in the destination collection, and false otherwise.

Returns:

  • The destination collection containing the filtered elements.

Understanding filterTo

The filterTo function iterates over each element in the source collection and applies the given predicate to it. If the predicate returns true, the element is added to the destination collection. This allows you to filter elements based on specific criteria and collect the results in a custom collection.

Examples

Basic Usage

To demonstrate the basic usage of filterTo, we will filter a list of integers to include only even numbers and store the result in a mutable list.

Example

fun main() { val numbers = listOf(1, 2, 3, 4, 5, 6) val evenNumbers = mutableListOf<Int>() numbers.filterTo(evenNumbers) { it % 2 == 0 } println("Even numbers: $evenNumbers") } 

Output:

Even numbers: [2, 4, 6] 

Filtering to a Mutable List

This example shows how to use filterTo to filter elements and store the result in a mutable list.

Example

fun main() { val fruits = listOf("apple", "banana", "cherry", "date") val longFruits = mutableListOf<String>() fruits.filterTo(longFruits) { it.length > 5 } println("Fruits with more than 5 characters: $longFruits") } 

Output:

Fruits with more than 5 characters: [banana, cherry] 

Filtering to a Mutable Set

This example demonstrates how to use filterTo to filter elements and store the result in a mutable set.

Example

fun main() { val numbers = listOf(1, 2, 2, 3, 4, 4, 5) val uniqueEvenNumbers = mutableSetOf<Int>() numbers.filterTo(uniqueEvenNumbers) { it % 2 == 0 } println("Unique even numbers: $uniqueEvenNumbers") } 

Output:

Unique even numbers: [2, 4] 

Real-World Use Case

Filtering Orders to a Custom Collection

In real-world applications, the filterTo function can be used to filter orders based on specific criteria and collect the results in a custom collection, such as a list or set.

Example

data class Order(val id: Int, val amount: Double, val status: String) fun main() { val orders = listOf( Order(1, 100.0, "Shipped"), Order(2, 150.0, "Pending"), Order(3, 200.0, "Shipped") ) val shippedOrders = mutableListOf<Order>() orders.filterTo(shippedOrders) { it.status == "Shipped" } println("Shipped orders: $shippedOrders") } 

Output:

Shipped orders: [Order(id=1, amount=100.0, status=Shipped), Order(id=3, amount=200.0, status=Shipped)] 

Conclusion

The filterTo function in Kotlin is a powerful and flexible way to filter elements of a collection based on a given predicate and store the filtered elements in a specified destination collection. It allows you to easily collect filtered results in custom collections, making it useful for various applications, including data processing and filtering user input. By understanding and using the filterTo function, you can effectively manage and process collections in your Kotlin applications.

Leave a Comment

Scroll to Top