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.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explains 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

  1. 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]

  1. 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

  1. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

Use tool

Image Converter

Convert between different image formats.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help