codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

9 / 68 topics
1Introduction to Kotlin2Kotlin Installation3Hello World Program4Variables and Data Types5Operators in Kotlin6Control Flow Statements7Functions in Kotlin8Strings and Templates9Collections in Kotlin10Null Safety
Tutorials/Kotlin/Collections in Kotlin
🎯Kotlin

Collections in Kotlin

Updated 2026-04-20
3 min read

Introduction

Collections are fundamental data structures used in programming to store and manage groups of objects. In Kotlin, collections provide a rich set of functionalities that simplify tasks such as iteration, filtering, mapping, and transforming data. This tutorial will cover the basics of collections in Kotlin, including lists, sets, maps, and their mutable counterparts.

Understanding Collections

Collections in Kotlin are categorized into two main types: mutable and immutable. Mutable collections can be modified after they are created, while immutable collections cannot be changed once they are initialized.

Common Collection Types

  1. List: An ordered collection that allows duplicate elements.
  2. Set: A collection that does not allow duplicate elements.
  3. Map: A collection of key-value pairs where each key is unique.

Lists in Kotlin

Lists are the most commonly used collections in Kotlin. They maintain the order of elements and can contain duplicates.

Creating Lists

Kotlin provides several ways to create lists:

// Immutable list
val immutableList = listOf("Apple", "Banana", "Cherry")

// Mutable list
val mutableList = mutableListOf("Apple", "Banana", "Cherry")

Accessing Elements

Elements in a list can be accessed using indices:

println(immutableList[0]) // Output: Apple
println(mutableList[1])  // Output: Banana

Adding and Removing Elements

Mutable lists allow adding and removing elements:

mutableList.add("Date")
mutableList.removeAt(2)

Sets in Kotlin

Sets are collections that do not allow duplicate elements. They are useful when you need to ensure uniqueness.

Creating Sets

Kotlin provides two types of sets: Set and MutableSet.

// Immutable set
val immutableSet = setOf("Apple", "Banana", "Cherry")

// Mutable set
val mutableSet = mutableSetOf("Apple", "Banana", "Cherry")

Adding and Removing Elements

Mutable sets allow adding and removing elements:

mutableSet.add("Date")
mutableSet.remove("Banana")

Maps in Kotlin

Maps store key-value pairs, where each key is unique. They are useful for associating data.

Creating Maps

Kotlin provides two types of maps: Map and MutableMap.

// Immutable map
val immutableMap = mapOf("Apple" to 10, "Banana" to 20, "Cherry" to 30)

// Mutable map
val mutableMap = mutableMapOf("Apple" to 10, "Banana" to 20, "Cherry" to 30)

Accessing Values

Values in a map can be accessed using keys:

println(immutableMap["Apple"]) // Output: 10
println(mutableMap["Banana"])  // Output: 20

Adding and Removing Entries

Mutable maps allow adding and removing entries:

mutableMap.put("Date", 40)
mutableMap.remove("Cherry")

Common Operations on Collections

Kotlin provides a rich set of functions for common operations on collections.

Iteration

Iterating over a collection is straightforward:

for (item in immutableList) {
    println(item)
}

Filtering

Filtering elements based on a condition:

val filteredList = immutableList.filter { it.startsWith("B") }
println(filteredList) // Output: [Banana]

Mapping

Transforming elements of a collection:

val upperCaseList = immutableList.map { it.toUpperCase() }
println(upperCaseList) // Output: [APPLE, BANANA, CHERRY]

Reducing

Reducing a collection to a single value:

val sumOfValues = mutableMap.values.sum()
println(sumOfValues) // Output: 60

Best Practices

  1. Use Immutable Collections When Possible: Immutable collections are safer and more predictable, especially in concurrent environments.
  2. Choose the Right Collection Type: Use List when order matters and duplicates are allowed. Use Set when uniqueness is required. Use Map for key-value associations.
  3. Leverage Kotlin's Standard Library Functions: Utilize functions like filter, map, and reduce to write concise and readable code.

Conclusion

Collections in Kotlin provide powerful tools for managing groups of objects. By understanding the differences between mutable and immutable collections, and by leveraging Kotlin's rich set of collection operations, you can write efficient and expressive code. Whether you're working on a small script or a large-scale application, mastering collections will greatly enhance your productivity.

Further Reading

  • Kotlin Collections Documentation
  • Effective Kotlin: Use standard library functions

This tutorial provides a comprehensive overview of collections in Kotlin, equipping you with the knowledge to effectively manage and manipulate data in your applications.


PreviousStrings and TemplatesNext Null Safety

Recommended Gear

Strings and TemplatesNull Safety