Data Structures: Collections and Data Classes in Kotlin

Back to course overview

Hi and welcome to another coding session.

After learning how to store data in a variable/value, we will now learn how to organize our data with Data Structures.

We will learn how to store multiple entries inside Lists, Sets, Arrays and Maps. This session will also show how you can logically organize your data using Data Classes.

Here is the video to follow along:

Video

References:

URLs:

Code:

fun main() {
    //Arrays
    val myStringArray = arrayOf("Marcus", "Eisele", "Developer")
    //Lists
    val myStringList = listOf("Marcus", "Eisele", "Developer", "Marcus")

    //Sets
    val myStringSet = setOf("Marcus", "Eisele", "Developer", "Marcus")

    println("The size of our List is ${myStringList.size}")
    println("The size of our Set is ${myStringSet.size}")

    //Maps
    val userMap = mapOf("user-001" to "Jon", Pair("user-002", "Marcus"))
    // user-001 -> Jon
    // user-002 -> Marcus
    println(userMap.entries)
    println("user-001 is ${userMap["user-001"]}")
    println("user-002 is ${userMap["user-002"]}")

    //Data Classes
    val personMap = mapOf("Marcus" to Person("Marcus", "Eisele", listOf("Mo", "eiselems")))
    println(personMap.entries)
}

data class Person(val firstName: String, val lastName: String, val nicknames: List<String>)

If you have any further questions, leave a comment either here on the Blog, on the Youtube-Video or join the Discord Community.