Kotlin / Collections and Generics
Using Lists, Sets, and Maps
In this tutorial, you'll delve into the specifics of Lists, Sets, and Maps in Kotlin. You'll learn how to create and use these data structures to store and manipulate data.
Section overview
5 resourcesExplains how to use Kotlin’s collection framework and work with generics.
Using Lists, Sets, and Maps in Kotlin
Introduction
In this tutorial, we aim to teach you the specifics of Lists, Sets, and Maps in Kotlin, which are fundamental data structures used to store and manipulate data in programming.
By the end of this guide, you will:
- Understand the differences between Lists, Sets, and Maps
- Know how to create and manipulate these data structures
- Be able to apply this knowledge in practical situations
Prerequisites: Basic understanding of Kotlin syntax and programming concepts.
Step-by-Step Guide
Lists
A List in Kotlin is an ordered collection of items. Each item in the list has an index, starting from 0.
To create a list, you can use the listOf function:
val names = listOf("John", "Sarah", "Mark")
Sets
A Set in Kotlin is an unordered collection of unique items. It does not contain duplicate items.
To create a set, you can use the setOf function:
val uniqueNumbers = setOf(1, 2, 3, 1)
Note that the duplicate 1 is only stored once in the set.
Maps
A Map in Kotlin is a collection of key-value pairs. Each key is unique and is associated with exactly one value.
To create a map, you can use the mapOf function:
val ages = mapOf("John" to 25, "Sarah" to 30)
Code Examples
Lists
Here's an example of creating and manipulating a list in Kotlin:
val numbers = mutableListOf(1, 2, 3, 4, 5) // mutable list allows modifications
numbers.add(6) // adds the number 6 to the end of the list
println(numbers) // prints [1, 2, 3, 4, 5, 6]
Sets
Here's an example of creating and manipulating a set in Kotlin:
val set = mutableSetOf(1, 2, 3, 1) // mutable set allows modifications
set.add(4) // adds the number 4 to the set
println(set) // prints [1, 2, 3, 4]
Maps
Here's an example of creating and manipulating a map in Kotlin:
val map = mutableMapOf("John" to 25, "Sarah" to 30) // mutable map allows modifications
map["Mark"] = 35 // adds a new key-value pair to the map
println(map) // prints {John=25, Sarah=30, Mark=35}
Summary
We've covered the basics of Lists, Sets, and Maps in Kotlin. You've learned how to create these data structures, add items to them, and print their contents.
The next steps for your learning could be understanding how to remove items from these collections, how to sort them, and how to search for specific items.
Additional resources:
- Kotlin Official Documentation
Practice Exercises
- Exercise: Create a list of your favourite movies, then print the list. Add another movie to the list and print it again.
Solution:
kotlin
val movies = mutableListOf("Inception", "Interstellar", "The Dark Knight")
println(movies) // prints [Inception, Interstellar, The Dark Knight]
movies.add("Dunkirk")
println(movies) // prints [Inception, Interstellar, The Dark Knight, Dunkirk]
- Exercise: Create a set of numbers, then print the set. Try adding a duplicate number to the set and print it again.
Solution:
kotlin
val numbers = mutableSetOf(1, 2, 3, 4, 5)
println(numbers) // prints [1, 2, 3, 4, 5]
numbers.add(3)
println(numbers) // still prints [1, 2, 3, 4, 5] because sets don't allow duplicates
- Exercise: Create a map of names and ages, then print the map. Add a new name-age pair to the map and print it again.
Solution:
kotlin
val ages = mutableMapOf("John" to 25, "Sarah" to 30)
println(ages) // prints {John=25, Sarah=30}
ages["Mark"] = 35
println(ages) // prints {John=25, Sarah=30, Mark=35}
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article