This Kotlin Collections and Functional Programming tutorial explains the Kotlin Collections framework, including lists (mutable and immutable), sets, maps, arrays, and utility functions. It also introduces functional programming concepts such as map, filter, reduce, and forEach. Each topic includes practical examples and best practices to help developers write clean, efficient, and readable Kotlin code for real-world applications.
Kotlin Collections and Functional Programming – Complete Tutorial
Collections Framework
Kotlin collections are divided into immutable (read-only) and mutable (modifiable) collections.
List
A List stores elements in a specific order.
Immutable List
val numbers = listOf(1, 2, 3, 4)
println(numbers) // [1, 2, 3, 4]
Mutable List
val mutableNumbers = mutableListOf(1, 2, 3)
mutableNumbers.add(4)
println(mutableNumbers) // [1, 2, 3, 4]
Best Practices
- Prefer immutable lists for safety and clarity.
- Use mutable lists only when you need to modify data.
Set
A Set stores unique elements with no particular order.
val uniqueNumbers = setOf(1, 2, 2, 3)
println(uniqueNumbers) // [1, 2, 3]
val mutableSet = mutableSetOf(1, 2, 3)
mutableSet.add(4)
println(mutableSet) // [1, 2, 3, 4]
Best Practices
- Use
Setto eliminate duplicates automatically. - Mutable sets are useful when elements need to change dynamically.
Map
A Map stores key-value pairs.
Immutable Map
val user = mapOf("name" to "Muni", "age" to 35)
println(user["name"]) // Muni
Mutable Map
val mutableUser = mutableMapOf("name" to "Muni")
mutableUser["age"] = 35
println(mutableUser) // {name=Muni, age=35}
Best Practices
- Use meaningful keys.
- Prefer immutable maps for read-only operations.
Array vs List
| FeatureArrayList | ||
| Size | Fixed | Dynamic (mutable) |
| Type | Homogeneous | Can store objects |
| Access | index-based | index-based |
| Functions | Limited | Rich collection functions |
Example
val arr = arrayOf(1, 2, 3)
val list = listOf(1, 2, 3, 4)
println(arr[0]) // 1
println(list[0]) // 1
Best Practices
- Use
Arraywhen size is fixed. - Use
Listfor flexible and functional operations.
Collection Utility Functions
Kotlin provides functional programming utilities for collections.
val numbers = listOf(1, 2, 3, 4, 5)
// forEach
numbers.forEach { println(it) }
// map
val squared = numbers.map { it * it }
println(squared) // [1, 4, 9, 16, 25]
// filter
val even = numbers.filter { it % 2 == 0 }
println(even) // [2, 4]
// reduce
val sum = numbers.reduce { acc, i -> acc + i }
println(sum) // 15
// any / all
println(numbers.any { it > 3 }) // true
println(numbers.all { it > 0 }) // true
Best Practices
- Use
map,filter,reducefor concise functional operations. - Prefer immutable collections with functional transformations for safer code.
- Chain collection functions for expressive code:
val result = numbers.filter { it % 2 == 0 }.map { it * 2 }
println(result) // [4, 8]
Summary
This chapter explained Kotlin’s collections framework including lists, sets, maps, arrays, and collection utility functions. It also introduced functional programming operations like map, filter, reduce, and forEach to manipulate collections efficiently. Mastery of these tools is essential for writing clean, concise, and functional Kotlin code.