Kotlin Sequence filterNot Function

The filterNot function in Kotlin is used to filter elements of a sequence based on a specified predicate, but it returns a sequence containing all elements that do not match the predicate. It is part of the Kotlin standard library and allows you to exclude elements that satisfy a given condition.

Table of Contents

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

Introduction

The filterNot function allows you to exclude elements from a sequence that satisfy a given predicate. This is useful for scenarios where you need to remove certain elements from a sequence based on a specific condition, enabling efficient data filtering and manipulation.

filterNot Function Syntax

The syntax for the filterNot function is as follows:

fun <T> Sequence<T>.filterNot(predicate: (T) -> Boolean): Sequence<T> 

Parameters:

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

Returns:

  • A sequence containing all elements that do not satisfy the given predicate.

Understanding filterNot

The filterNot function works by iterating through the sequence and applying the predicate to each element. Elements that do not satisfy the predicate are included in the resulting sequence, while those that do are excluded.

Examples

Basic Usage

To demonstrate the basic usage of filterNot, we will create a sequence of integers and exclude all even numbers.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6) val oddNumbers = numbers.filterNot { it % 2 == 0 } println(oddNumbers.toList()) // Output: [1, 3, 5] } 

Output:

[1, 3, 5] 

Filtering a Sequence of Strings

This example shows how to filter a sequence of strings to exclude those with a length greater than 5.

Example

fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha") val shortNames = names.filterNot { it.length > 5 } println(shortNames.toList()) // Output: [Arjun, Esha] } 

Output:

[Arjun, Esha] 

Using filterNot with Custom Objects

You can use the filterNot function to exclude 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 adults = people.filterNot { it.age < 25 } println(adults.toList()) // Output: [Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Deepak, age=28), Person(name=Esha, age=26)] } 

Output:

[Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Deepak, age=28), Person(name=Esha, age=26)] 

Chaining filterNot with Other Functions

The filterNot function can be chained with other sequence functions to perform more complex operations before or after filtering the elements.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val result = numbers.filterNot { it % 2 == 0 } .map { it * 2 } println(result.toList()) // Output: [2, 6, 10, 14, 18] } 

Output:

[2, 6, 10, 14, 18] 

Real-World Use Case

Filtering Out Low-Priced Products

In real-world applications, the filterNot function can be used to exclude 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 expensiveProducts = products.filterNot { it.price < 300 } println(expensiveProducts.toList()) // Output: [Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99)] } 

Output:

[Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99)] 

Conclusion

The filterNot function in Kotlin provides used for excluding elements from a sequence based on a specified predicate. By understanding and using the filterNot function, you can efficiently manage and process data in your Kotlin applications, ensuring that you exclude elements that satisfy certain conditions according to your requirements.

Leave a Comment

Scroll to Top