Skip to content

Instantly share code, notes, and snippets.

View sajjadyousefnia's full-sized avatar
🤒
Out sick

Sajjad Yousefnia sajjadyousefnia

🤒
Out sick
View GitHub Profile
var mutableNames1 = names.toMutableList()
mutableNames1.add("Ruth") // now mutable and added "Ruth" to list
// a mutable list of a certain type e.g. String
val mutableListNames: MutableList<String> = mutableListOf<String>("Josh", "Kene", "Sanya")
mutableListNames.add("Mary")
mutableListNames.removeAt(1)
mutableListNames[0] = "Oluchi" // replaces the element in index 0 with "Oluchi"
// a mutable list of mixed types
val mutableListMixed = mutableListOf("BMW", "Toyota", 1, 6.76, 'v')
val mutableListFood: MutableList<String> = mutableListOf<String>("Rice & stew", "Jollof rice", "Eba & Egusi", "Fried rice")
mutableListFood.remove("Fried rice")
mutableListFood.removeAt(0)
mutableListFood.set(0, "Beans")
mutableListFood.add(1, "Bread & tea")
for (foodName in mutableListFood) {
println(foodName)
}
Beans
Bread & tea
Eba & Egusi
// creates a immutable set of mixed types
val mixedTypesSet = setOf(2, 4.454, "how", "far", 'c') // will compile
var intSet: Set<Int> = setOf(1, 3, 4) // only integers types allowed
val intsHashSet: java.util.HashSet<Int> = hashSetOf(1, 2, 6, 3)
intsHashSet.add(5)
intsHashSet.remove(1)
val intsSortedSet: java.util.TreeSet<Int> = sortedSetOf(4, 1, 7, 2)
intsSortedSet.add(6)
intsSortedSet.remove(1)
intsSortedSet.clear()
val intsLinkedHashSet: java.util.LinkedHashSet<Int> = linkedSetOf(5, 2, 7, 2, 5) // 5, 2, 7
intsLinkedHashSet.add(4)
intsLinkedHashSet.remove(2)
intsLinkedHashSet.clear()
// creates a mutable set of int types only
val intsMutableSet: MutableSet<Int> = mutableSetOf(3, 5, 6, 2, 0)
intsMutableSet.add(8)
intsMutableSet.remove(3)
val callingCodesMap: Map<Int, String> = mapOf(234 to "Nigeria", 1 to "USA", 233 to "Ghana")
for ((key, value) in callingCodesMap) {
println("$key is the calling code for $value")
}
print(callingCodesMap[234]) // Nigeria