🎓 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 toList 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
toListFunction Syntax- Understanding
toList - Examples
- Basic Usage
- Using
toListwith Custom Types - Modifying the Resulting List
- Real-World Use Case
- Conclusion
Introduction
The toList function converts an array into a List collection. The resulting list is immutable, meaning that it cannot be modified. This function is useful for leveraging the features and methods of the List interface on an array.
toList Function Syntax
The syntax for the toList function is as follows:
fun <T> Array<out T>.toList(): List<T>
Parameters:
- This function does not take any parameters.
Returns:
- An immutable list containing the elements of the original array.
Understanding toList
The toList function creates an immutable list that contains the elements of the original array. Unlike asList, which creates a list view of the array, toList creates a separate list that does not reflect changes to the original array.
Examples
Basic Usage
To demonstrate the basic usage of toList, 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.toList()
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 toList with Custom Types
This example shows how to use toList 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.toList()
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 Resulting List
This example demonstrates how to handle attempts to modify the resulting list, which is immutable.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val numbersList = numbers.toList()
try {
numbersList.add(6)
} catch (e: UnsupportedOperationException) {
println("Error: ${e.message}")
}
println("Original array: ${numbers.joinToString()}")
println("Immutable list: $numbersList")
}
Output:
Error: UnsupportedOperationException
Original array: 1, 2, 3, 4, 5
Immutable list: [1, 2, 3, 4, 5]
Real-World Use Case
Converting an Array to a List for Collection Operations
In real-world applications, the toList function can be used to convert an array into an immutable list to leverage the powerful collection operations available in Kotlin.
Example
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val numbersList = numbers.toList()
val evenNumbers = numbersList.filter { it % 2 == 0 }
println("Even numbers: $evenNumbers")
}
Output:
Even numbers: [2, 4]
Conclusion
The toList function in Kotlin is a convenient method for creating an immutable list from an array. It allows for leveraging the powerful features and methods of the List interface on an array while ensuring that the resulting list cannot be modified. 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