Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Kotlin Collections Tutorial

Introduction to Collections

Kotlin Collections are a set of data structures that allow you to store and manipulate groups of related data. Collections can be categorized into two main types: Mutable and Immutable.

Immutable collections cannot be modified after they are created, while mutable collections can be changed, allowing for addition, removal, or modification of elements.

Types of Collections

Kotlin provides several types of collections:

  • List: An ordered collection that can contain duplicates.
  • Set: An unordered collection that does not allow duplicates.
  • Map: A collection of key-value pairs, where each key is unique.

Working with Lists

Creating Lists

Lists in Kotlin can be created using the listOf() function for immutable lists and mutableListOf() for mutable lists.

Example:

val immutableList = listOf("Apple", "Banana", "Cherry")
val mutableList = mutableListOf("Apple", "Banana", "Cherry")

Accessing Elements

You can access elements in a list using their index, starting from zero.

Example:

val firstFruit = immutableList[0] // Apple

Modifying Mutable Lists

Mutable lists allow you to add or remove elements.

Example:

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

Working with Sets

Creating Sets

Sets can be created using the setOf() function for immutable sets and mutableSetOf() for mutable sets.

Example:

val immutableSet = setOf("Apple", "Banana", "Cherry")
val mutableSet = mutableSetOf("Apple", "Banana", "Cherry")

Adding and Removing Elements

Like mutable lists, mutable sets allow you to modify their contents.

Example:

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

Working with Maps

Creating Maps

Maps can be created using the mapOf() function for immutable maps and mutableMapOf() for mutable maps.

Example:

val immutableMap = mapOf("A" to "Apple", "B" to "Banana")
val mutableMap = mutableMapOf("A" to "Apple", "B" to "Banana")

Accessing and Modifying Entries

You can access a value using its corresponding key and add or remove key-value pairs in mutable maps.

Example:

val fruit = immutableMap["A"] // Apple
mutableMap["C"] = "Cherry"
mutableMap.remove("B")

Collection Operations

Kotlin collections come with a rich set of functions for performing operations like filtering, mapping, and folding.

Filtering

You can filter collections based on certain conditions using the filter() function.

Example:

val filteredList = immutableList.filter { it.startsWith("A") }

Mapping

Mapping allows you to transform elements in a collection using the map() function.

Example:

val mappedList = immutableList.map { it.toUpperCase() }

Reducing

The fold() function is used to accumulate values from a collection into a single result.

Example:

val totalLength = immutableList.fold(0) { acc, str -> acc + str.length }

Conclusion

Kotlin collections provide a powerful and flexible way to work with groups of data. Understanding how to use lists, sets, and maps, along with their operations, will help you build efficient and effective Kotlin applications. Experiment with these collections to become familiar with their features and functionalities.