DEV Community

Coder
Coder

Posted on • Originally published at itscoderslife.wordpress.com on

Android-Kotlin Digest #3 – A gist of Collection types

Today in Kotlin, learnt about Arrays, Hashmaps, ArrayList, MutableList, MapOf, ArrayOf. Here is gist of things.

In Swift we have all the functionalities of Array in one place.

In here Kotlin we may need to use combination of Array and ArrayList.

Here are few Kotlin examples:

Array :

var myArray = Array<Int>(6){2}
for (element in myArray) {
println("Item - " + element)
}

for (index in 0..myArray.size - 1) {

println(myArray[index])

}

ArrayList :

var myArrayList = ArrayList<String>()
myArrayList.add("It's")
myArrayList.add("Coders")
myArrayList.add("Life")

if (myArrayList.contains("Coders"))

println("He's a rockstar!")

myArrayList.remove("Coders")

myArrayList.add("Dams")

//iterating through objects

for (names in myArrayList) {

println("Names:" + names)

}

HashMap : (Dictionary in Swift)

var hashmap = HashMap<String, String>()
hashmap.put("Mary", "Married")
hashmap.put("Paulo", "Married")
hashmap.put("John", "Single")

for (k in hashmap.keys)

println(hashmap.get(k))

Other Collection Utilities:

var myArrayList = listOf<String>("Me", "James", "Bonni", "Life")
var mutableList = mutableListOf<String>("Me", "They", "James")
var myHashmap = hashMapOf(1 to "Paulo", 2 to "James")

happyCoding();

Top comments (0)