🎓 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
In this guide, we will see how to use the filter() function to filter lists in Kotlin.
What is List Filtering in Kotlin?
Filtering refers to the process of selecting items from a list that satisfy a given predicate (a function that returns true or false for a given input). In Kotlin, the filter function is employed for this purpose.
Basic Syntax:
fun <T> List<T>.filter(predicate: (T) -> Boolean): List<T>Examples with Outputs
Basic Filtering
Filtering out even numbers from a list:
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.filter { it % 2 == 0 }
println(evens) // Output: [2, 4]Filtering Based on Index
Using filterIndexed to filter based on the item and its index:
val letters = listOf('a', 'b', 'c', 'd')
val filtered = letters.filterIndexed { index, char -> index > 1 }
println(filtered) // Output: [c, d]Filtering Out Nulls
Conveniently remove nulls from a list using filterNotNull:
val nullableList = listOf(1, 2, null, 4, null)
val nonNulls = nullableList.filterNotNull()
println(nonNulls) // Output: [1, 2, 4]Filtering Using Custom Objects
Suppose you have a list of Person objects and want to filter by age:
data class Person(val name: String, val age: Int)
val people = listOf(Person("Alice", 28), Person("Bob", 25), Person("Charlie", 30))
val youngPeople = people.filter { it.age < 29 }
println(youngPeople) // Output: [Person(name=Alice, age=28), Person(name=Bob, age=25)]Negated Filtering with filterNot
Sometimes it's more readable to filter items that don't match a condition:
val items = listOf(1, 2, 3, 4, 5)
val notEvens = items.filterNot { it % 2 == 0 }
println(notEvens) // Output: [1, 3, 5]Advanced Examples
Combining Filters
You can chain multiple filter operations for more complex conditions:
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
val result = numbers.filter { it > 3 }.filter { it % 2 == 0 }
println(result) // Output: [4, 6, 8]Using partition for Filter and FilterNot in One
If you want both filtered and unfiltered results, use partition:
val numbers = listOf(1, 2, 3, 4, 5)
val (evens, odds) = numbers.partition { it % 2 == 0 }
println(evens) // Output: [2, 4]
println(odds) // Output: [1, 3, 5]Conclusion
Kotlin's approach to list filtering epitomizes both clarity and efficiency. Whether you're a seasoned Kotlin developer or just beginning your journey, the provided filter examples are sure to augment your toolkit for list processing. Happy coding!
Related Kotlin Posts
- Kotlin List
- Kotlin Set
- Kotlin listOf
- Kotlin mutableListOf
- Kotlin mapOf
- Kotlin mutableMapOf
- Kotlin setOf
- Kotlin mutableSetOf
- Kotlin sequenceOf
- Kotlin list filter
- Kotlin list map
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