🎓 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 asReversed function in Kotlin is used to create a view of a list in reversed order. This function belongs to the List class in the Kotlin standard library and provides a straightforward way to access elements of a list from the end to the beginning without modifying the original list.
Table of Contents
- Introduction
asReversedFunction Syntax- Examples
- Basic Usage
- Using
asReversedwith Mutable Lists - Combining
asReversedwith Other List Operations
- Real-World Use Case
- Conclusion
Introduction
The asReversed function provides a way to view a list in reverse order. This is useful for scenarios where you need to process or display list elements in reverse without altering the original list.
asReversed Function Syntax
The syntax for the asReversed function is as follows:
fun <T> List<T>.asReversed(): List<T>
Parameters:
- This function does not take any parameters.
Returns:
- A read-only view of the original list in reverse order.
Examples
Basic Usage
To demonstrate the basic usage of asReversed, we will create a list and view its elements in reverse order.
Example
fun main() {
val numbers = listOf(1, 2, 3, 4, 5)
val reversedNumbers = numbers.asReversed()
println("Original list: $numbers")
println("Reversed view: $reversedNumbers")
}
Output:
Original list: [1, 2, 3, 4, 5]
Reversed view: [5, 4, 3, 2, 1]
Using asReversed with Mutable Lists
This example shows how asReversed works with mutable lists. Note that asReversed itself returns a read-only view, so any modifications to the original list will be reflected in the reversed view.
Example
fun main() {
val mutableNumbers = mutableListOf(10, 20, 30, 40, 50)
val reversedNumbers = mutableNumbers.asReversed()
println("Original mutable list: $mutableNumbers")
println("Reversed view: $reversedNumbers")
mutableNumbers.add(60)
println("Modified original list: $mutableNumbers")
println("Updated reversed view: $reversedNumbers")
}
Output:
Original mutable list: [10, 20, 30, 40, 50]
Reversed view: [50, 40, 30, 20, 10]
Modified original list: [10, 20, 30, 40, 50, 60]
Updated reversed view: [60, 50, 40, 30, 20, 10]
Combining asReversed with Other List Operations
This example demonstrates how to combine asReversed with other list operations, such as filtering and mapping.
Example
fun main() {
val names = listOf("Alice", "Bob", "Charlie", "Dave")
val filteredReversedNames = names.asReversed().filter { it.length > 3 }.map { it.uppercase() }
println("Filtered and reversed names: $filteredReversedNames")
}
Output:
Filtered and reversed names: [DAVE, CHARLIE, ALICE]
Real-World Use Case
Displaying Recent Activity
In real-world applications, the asReversed function can be used to display recent activity logs or messages, where the most recent entries should appear at the top.
Example
fun main() {
val activityLog = listOf("Logged in", "Viewed profile", "Logged out", "Signed up")
val recentActivity = activityLog.asReversed()
println("Recent activity: $recentActivity")
}
Output:
Recent activity: [Signed up, Logged out, Viewed profile, Logged in]
Conclusion
The asReversed function returns a view of the list where the elements appear in reverse order. It does not create a new list or modify the original list; instead, it provides a new view that reads elements from the end to the beginning.
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