🎓 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 asList function in Kotlin is used to create a list from an array. This function is part of the Kotlin standard library and provides a straightforward way to convert an array into a list.
Table of Contents
- Introduction
asListFunction Syntax- Understanding
asList - Examples
- Basic Usage
- Using
asListwith Custom Types - Modifying the List
- Real-World Use Case
- Conclusion
Introduction
The asList function converts an array into a List collection. The resulting list is backed by the original array, meaning that changes to the array will reflect in the list and vice versa. This function is useful for utilizing the powerful features and methods of the List interface on an array.
asList Function Syntax
The syntax for the asList function is as follows:
fun <T> Array<out T>.asList(): List<T>
Parameters:
- This function does not take any parameters.
Returns:
- A list containing the elements of the original array.
Understanding asList
The asList function creates a list view of the original array. Any changes made to the array will be reflected in the list, and changes made to the list will affect the array. The list created by asList is mutable if the original array is mutable.
Examples
Basic Usage
To demonstrate the basic usage of asList, we will create an array of integers and convert it into a list.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val numbersList = numbers.asList()
println("Original array: ${numbers.joinToString()}")
println("Converted list: $numbersList")
}
Output:
Original array: 1, 2, 3, 4, 5
Converted list: [1, 2, 3, 4, 5]
Using asList with Custom Types
This example shows how to use asList with an array of custom objects.
Example
data class Person(val name: String, val age: Int)
fun main() {
val people = arrayOf(
Person("Ravi", 25),
Person("Anjali", 30),
Person("Priya", 22)
)
val peopleList = people.asList()
println("Original array: ${people.joinToString()}")
println("Converted list: $peopleList")
}
Output:
Original array: Person(name='Ravi', age=25), Person(name='Anjali', age=30), Person(name='Priya', age=22)
Converted list: [Person(name='Ravi', age=25), Person(name='Anjali', age=30), Person(name='Priya', age=22)]
Modifying the List
This example demonstrates how modifying the list created by asList affects the original array.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val numbersList = numbers.asList()
numbers[0] = 10
println("Modified array: ${numbers.joinToString()}")
println("List after modifying array: $numbersList")
// Note: numbersList.add(6) will throw an UnsupportedOperationException because the list is backed by the array.
}
Output:
Modified array: 10, 2, 3, 4, 5
List after modifying array: [10, 2, 3, 4, 5]
Real-World Use Case
Converting an Array to a List for Collection Operations
In real-world applications, the asList function can be used to convert an array into a list to leverage the powerful collection operations available in Kotlin.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val numbersList = numbers.asList()
val evenNumbers = numbersList.filter { it % 2 == 0 }
println("Even numbers: $evenNumbers")
}
Output:
Even numbers: [2, 4]
Conclusion
The asList function in Kotlin is a convenient method for creating a list view of an array. It allows for leveraging the powerful features and methods of the List interface on an array while maintaining a connection between the array and the list. By understanding and using this function, you can effectively manage array-to-list conversions and utilize collection 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