🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The filterNot function in Kotlin is used to filter elements of a collection based on a given predicate, returning a new list containing only the elements that do not match the specified condition. This function belongs to the Kotlin standard library and provides a convenient way to exclude certain elements from a collection.
Table of Contents
- Introduction
filterNotFunction Syntax- Understanding
filterNot - Examples
- Basic Usage
- Filtering a List of Strings
- Filtering with Custom Conditions
- Real-World Use Case
- Conclusion
Introduction
The filterNot function allows you to filter elements of a collection (such as a list) based on a given predicate, creating a new list that excludes elements matching the specified condition. This function is useful for removing specific elements from a collection based on custom criteria.
filterNot Function Syntax
The syntax for the filterNot function is as follows:
fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T>
Parameters:
predicate: A lambda function that takes an element of the collection as input and returnstrueif the element should be excluded from the resulting list, andfalseotherwise.
Returns:
- A new list containing only the elements that do not match the specified predicate.
Understanding filterNot
The filterNot function iterates over each element in the collection and applies the given predicate to it. If the predicate returns true, the element is excluded from the resulting list; if it returns false, the element is included. This allows you to create a subset of the original collection that excludes specific elements based on certain criteria.
Examples
Basic Usage
To demonstrate the basic usage of filterNot, we will filter a list of integers to exclude even numbers.
Example
fun main() {
val numbers = listOf(1, 2, 3, 4, 5, 6)
val oddNumbers = numbers.filterNot { it % 2 == 0 }
println("Odd numbers: $oddNumbers")
}
Output:
Odd numbers: [1, 3, 5]
Filtering a List of Strings
This example shows how to use filterNot to create a new list containing only strings that do not meet a certain condition.
Example
fun main() {
val fruits = listOf("apple", "banana", "cherry", "date")
val shortFruits = fruits.filterNot { it.length > 5 }
println("Fruits with 5 or fewer characters: $shortFruits")
}
Output:
Fruits with 5 or fewer characters: [apple, date]
Filtering with Custom Conditions
This example demonstrates how to use filterNot with custom conditions to filter a list of objects.
Example
data class Person(val name: String, val age: Int)
fun main() {
val people = listOf(
Person("Alice", 30),
Person("Bob", 25),
Person("Charlie", 35)
)
val nonAdults = people.filterNot { it.age >= 30 }
println("Non-adults: $nonAdults")
}
Output:
Non-adults: [Person(name=Bob, age=25)]
Real-World Use Case
Filtering a List of Orders
In real-world applications, the filterNot function can be used to filter a list of orders based on their status or other criteria, such as excluding certain types of orders.
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 nonShippedOrders = orders.filterNot { it.status == "Shipped" }
println("Non-shipped orders: $nonShippedOrders")
}
Output:
Non-shipped orders: [Order(id=2, amount=150.0, status=Pending)]
Conclusion
The filterNot function in Kotlin is a powerful and convenient way to create a new list containing only the elements that do not match a specified predicate. It allows you to easily exclude specific elements from a collection based on custom criteria, making it useful for various applications, including data processing, filtering user input, and more.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment