Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
listOf()
is a function in Kotlin that allows you to create an immutable list (a list that cannot be modified after its creation).
Let's dive into how to use it with examples and explanations:
val numbers = listOf(1, 2, 3, 4, 5) println(numbers) // prints [1, 2, 3, 4, 5]
Immutable: The list returned by listOf()
is read-only. This means that you cannot add or remove items from it.
val fruits = listOf("apple", "banana", "cherry") // fruits.add("grape") // This will result in a compile error
Nullable and Non-Nullable Lists: You can create lists that contain nullable elements.
val nullableList: List<String?> = listOf("apple", null, "cherry") println(nullableList) // prints [apple, null, cherry]
Empty List: If you invoke listOf()
without any arguments, you get an empty list.
val emptyList = listOf<String>() println(emptyList.isEmpty()) // prints true
Mixed Type Lists: Though it's not recommended for typical use-cases because of type safety, you can create a list of mixed types using listOf<Any>()
.
val mixedList = listOf<Any>(1, "two", 3.0) println(mixedList) // prints [1, two, 3.0]
Access Elements: Use indexing or methods to access elements.
val animals = listOf("cat", "dog", "elephant") println(animals[1]) // prints "dog"
You can combine listOf()
with other Kotlin collection functions to create more complex expressions:
Filtering:
val numbers = listOf(1, 2, 3, 4, 5) val evenNumbers = numbers.filter { it % 2 == 0 } println(evenNumbers) // prints [2, 4]
Mapping:
val words = listOf("a", "be", "see") val wordLengths = words.map { it.length } println(wordLengths) // prints [1, 2, 3]
Chaining:
val results = listOf(1, 2, 3, 4, 5) .filter { it % 2 == 0 } .map { it * it } println(results) // prints [4, 16] (squared of even numbers)
In summary, listOf()
provides a way to quickly create immutable lists in Kotlin. Immutable lists are a great way to ensure that data isn't unexpectedly modified, providing an extra layer of safety in your programs. If you need a mutable list, Kotlin provides the mutableListOf()
function which offers add, remove, and other modification operations.
Creating a list in Kotlin with listOf()
:
listOf()
function.val numbersList = listOf(1, 2, 3, 4, 5)
Immutable lists in Kotlin:
listOf()
are immutable, meaning their size and elements cannot be changed.// This would result in a compilation error numbersList.add(6)
Initializing and declaring lists with listOf()
in Kotlin:
listOf()
in a single line.val fruits = listOf("Apple", "Banana", "Orange")
Adding and removing elements in a Kotlin list:
listOf()
creates immutable lists, elements cannot be added or removed.// Attempting to add or remove elements would result in a compilation error
Accessing elements in a list created with listOf()
:
val firstElement = numbersList[0]
Iterating over a list in Kotlin:
for (number in numbersList) { // Process each number }
Filtering and mapping with listOf()
in Kotlin:
filter
and map
can be used for data transformations.val evenNumbers = numbersList.filter { it % 2 == 0 } val squaredNumbers = numbersList.map { it * it }
List functions and methods in Kotlin:
val size = numbersList.size val lastIndex = numbersList.lastIndex
Null safety and listOf()
in Kotlin:
val nullableList: List<String?> = listOf(null, "Hello", null)
listOf()
vs mutableListOf()
in Kotlin:
listOf()
creates immutable lists, while mutableListOf()
creates mutable lists.val mutableList = mutableListOf(1, 2, 3) mutableList.add(4) // Works fine
Using listOf()
with different data types in Kotlin:
listOf()
can be used with various data types within the same list.val mixedList = listOf(1, "Two", 3.0, true)
Copying and transforming lists in Kotlin:
toList()
and toMutableList()
.val copyList = numbersList.toList() val mutableCopyList = numbersList.toMutableList()