🎓 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 findLast function in Kotlin is used to find the last element in an array that matches a given predicate. This function is part of the Kotlin standard library and provides a straightforward way to search for elements based on a condition, starting from the end of the array.
Table of Contents
- Introduction
findLastFunction Syntax- Understanding
findLast - Examples
- Basic Usage
- Using
findLastwith Custom Types - Handling Non-Existing Elements
- Real-World Use Case
- Conclusion
Introduction
The findLast function returns the last element in the array that matches the given predicate, or null if no such element is found. It is a simple and effective way to search for elements based on a condition from the end of the array.
findLast Function Syntax
The syntax for the findLast function is as follows:
inline fun <T> Array<out T>.findLast(predicate: (T) -> Boolean): T?
Parameters:
predicate: A lambda function that takes an element of typeTand returnstrueif the element matches the condition,falseotherwise.
Returns:
- The last element that matches the given predicate, or
nullif no such element is found.
Understanding findLast
The findLast function iterates through the array from the end to the beginning and returns the last element that matches the given predicate. If no element matches the predicate, it returns null.
Examples
Basic Usage
To demonstrate the basic usage of findLast, we will create an array of integers and find the last element that is greater than 3.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val lastGreaterThanThree = numbers.findLast { it > 3 }
println("Last element greater than 3: $lastGreaterThanThree")
}
Output:
Last element greater than 3: 5
Using findLast with Custom Types
This example shows how to use findLast to search for the last occurrence of a custom object in an array based on a condition.
Example
data class Person(val name: String, val age: Int)
fun main() {
val people = arrayOf(
Person("Ravi", 25),
Person("Anjali", 30),
Person("Priya", 22),
Person("Ravi", 35)
)
val lastPersonNamedRavi = people.findLast { it.name == "Ravi" }
println("Last person named Ravi: $lastPersonNamedRavi")
}
Output:
Last person named Ravi: Person(name='Ravi', age=35)
Handling Non-Existing Elements
This example demonstrates how to handle cases where no element matches the given predicate.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val lastGreaterThanFive = numbers.findLast { it > 5 }
if (lastGreaterThanFive != null) {
println("Last element greater than 5: $lastGreaterThanFive")
} else {
println("No element greater than 5 found")
}
}
Output:
No element greater than 5 found
Real-World Use Case
Searching for the Last Item in a List
In real-world applications, the findLast function can be used to search for the last item in a list based on specific conditions, such as finding the last product that meets certain criteria.
Example
data class Product(val name: String, val price: Double)
fun main() {
val products = arrayOf(
Product("Laptop", 999.99),
Product("Smartphone", 699.99),
Product("Tablet", 299.99),
Product("Smartphone", 749.99)
)
val lastExpensiveSmartphone = products.findLast { it.name == "Smartphone" && it.price > 700 }
println("Last expensive Smartphone: $lastExpensiveSmartphone")
}
Output:
Last expensive Smartphone: Product(name='Smartphone', price=749.99)
Conclusion
The findLast function in Kotlin is a convenient method for searching for the last element in an array that matches a given predicate. It provides a simple way to locate elements based on conditions from the end of the array and handle cases where the element may or may not be found.
By understanding and using this function, you can effectively manage search operations in your Kotlin applications.
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