Kotlin Collections and Functional Programming Tutorial: List, Set, Map, Arrays, and Collection Utilities


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

  1. Prefer immutable lists for safety and clarity.
  2. 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

  1. Use Set to eliminate duplicates automatically.
  2. 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

  1. Use meaningful keys.
  2. Prefer immutable maps for read-only operations.

Array vs List

FeatureArrayList
SizeFixedDynamic (mutable)
TypeHomogeneousCan store objects
Accessindex-basedindex-based
FunctionsLimitedRich 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

  1. Use Array when size is fixed.
  2. Use List for 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

  1. Use map, filter, reduce for concise functional operations.
  2. Prefer immutable collections with functional transformations for safer code.
  3. 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.