Day51 of #100DaysOfCode

Kushagra Kesav
3 min readMar 29, 2022

--

#CodeTogether Day 51/100

Hii folks 🙌

Today I will be starting a new Unit of Android Basic with Kotlin.

Unit 3: Navigation

Pathway 1: Navigate between screens

Source: https://developer.android.com/courses/android-basics-kotlin/course

Collections in Kotlin

A collection is a group of related items, like a list of words, or a set of employee records. The collection can have the items ordered or unordered, and the items can be unique or not.

As with lists, Kotlin distinguishes between mutable and immutable collections. Kotlin provides numerous functions for adding or deleting items, viewing, and manipulating collections.

Create a list

  • We’ll review creating a list of numbers and sorting them.
fun main() {
val numbers = listOf(0, 3, 8, 4, 0, 5, 5, 8, 9, 2)
println("list: ${numbers}")
println("sorted: ${numbers.sorted()}")
}
list: [0, 3, 8, 4, 0, 5, 5, 8, 9, 2]
sorted: [0, 0, 2, 3, 4, 5, 5, 8, 8, 9]
  • Now we are going to learn about sets which is another type of collection in Kotlin. It’s a group of related items, but unlike a list, there can’t be any duplicates, and the order doesn’t matter. An item can be in the set or not, but if it’s in the set, there is only one copy of it. This is similar to the mathematical concept of a set.
  • The following code will convert the list to a set:
val setOfNumbers = numbers.toSet()
println("set: ${setOfNumbers}")
  • Now we will run to see the output:
list:   [0, 3, 8, 4, 0, 5, 5, 8, 9, 2]
sorted: [0, 0, 2, 3, 4, 5, 5, 8, 8, 9]
set: [0, 3, 8, 4, 5, 9, 2]
  • Now we will learn about the mutable and immutable sets. We will initialize them with the same set of numbers but in a different order by adding these lines:
val set1 = setOf(1,2,3)
val set2 = mutableSetOf(3,2,1)
println("$set1 == $set2: ${set1 == set2}")
println("contains 7: ${setOfNumbers.contains(7)}")
  • After running the program these are the following output
[1, 2, 3] == [3, 2, 1]: true
contains 7: false
  • Now we will move forward to learn about maps which is also the type of collection that is a set of key-value pairs, designed to make it easy to look up a value given a particular key. Keys are unique, and each key maps to exactly one value, but the values can have duplicates. Values in a map can be strings, numbers, or objects — even another collection like a list or a set.
fun main() {
val peopleAges = mutableMapOf<String, Int>(
"Fred" to 30,
"Ann" to 23
)
println(peopleAges)
}
Output:{Fred=30, Ann=23}
  • To add more entries to the map, we can use the put() function, passing in the key and the value:
peopleAges.put("Barbara", 42)
  • We can also use a shorthand notation to add entries:
peopleAges["Joe"] = 51
  • Here is all of the above code:
fun main() {
val peopleAges = mutableMapOf<String, Int>(
"Fred" to 30,
"Ann" to 23
)
peopleAges.put("Barbara", 42)
peopleAges["Joe"] = 51
println(peopleAges)
}
Output:
{Fred=30, Ann=23, Barbara=42, Joe=51}

Today I Learned:

  • A collection is a group of related items.
  • Collections can be mutable or immutable.
  • Collections can be ordered or unordered.
  • Collections can require unique items or allow duplicates.
  • Kotlin supports different kinds of collections including lists, sets, and maps.

That is all for Day51 ✅

Thanks for reading, See you tomorrow!

--

--