Friday, August 18, 2017

Kotlin Collection Example

Welcome to kotlin Series

In the tutorial, we are going to discuss Kotlin collection.
we touch on below topics
1. Hashmap
2. Array
3. List
       3.a. mutable list
       3.b. immutable list
4. Set
       4.a. mutable set
       4.b. immutable set

Let's start-

Hashmap: In kotlin, we can use java hashmap easily. But kotlin has an own hashmap. First, we go through Java hashmap create an instance of Hashmap class and parameter will be string and string after input some data and print data to check.
see the code-
val hasMap = HashMap<String,String>()

hasMap.put("s","Shudipto")
hasMap["d"] = "trafder"
hasMap["f"] = "Himu"
hasMap["g"] = "Rupa"
now print a value
println(hasMap["s"])
Output:
Shudipto
I am using a for loop to print all the value that stored in hashmap with their key. code-
for ((v,k) in hasMap.entries){
        println("$v -> $k")
    }
Output:
s -> Shudipto
d -> trafder
f -> Himu
g -> Rupa
that's it in this way we use java hashmap. But kotlin has already a default hashmap. now use kotlin hashmap.
See the code
val hasmap = hashMapOf(1 to "shudipto")
hasmap.put(2,"trafder")
hasmap.put(3,"himu")
print a value
println(hasmap[1])
Output:
shudipto
use a for loop to see all the value with the key that stored in the hashmap.
//print all value with index
for ((position,value) in hasmap.entries){
    println("Index:$position value:$value")
}
Output:
//print all value with index
for ((position,value) in hasmap.entries){
    println("Index:$position value:$value")
}
That's all on hashmap. next topics Array

Array: kotlin has a special array class. let's see the example. But before jump into code, you can check our two tutorial about ArrayList and Array and loop. This will help you to understand the array concept easily.
see the code-
println("\nKotlin Array")
val array = arrayOf("Shudipto",1,2,3.5,"Trafder")
array[4] = "himu"
now print all the value with for loop-
for ((p,v) in array.withIndex()){
    println("Index:$p value:$v")
}
Output:
Index:0 value:Shudipto
Index:1 value:1
Index:2 value:2
Index:3 value:3.5
Index:4 value:himu
That's it on Array. Next topics is  list

List:  In kotlin, there are two types of list available. Mutable and immutable list.
see the code-
mutable list-
println("\n\nMutable list of array")
val list = mutableListOf(1,2,34,5,6,7,8,"Shudipto")

list[0] = "trafder"
immutable list-
val list2 = listOf(1,2,34,5,6,7,8,"Shudipto")

Mutable vs immutable list: you can not update immutable list after assign.

now print all the value by using for loop
mutable list-
for ((p,v) in list.withIndex()){
    println("Index:$p value:$v")
}
Output:
Mutable list of array
Index:0 value:trafder
Index:1 value:2
Index:2 value:34
Index:3 value:5
Index:4 value:6
Index:5 value:7
Index:6 value:8
Index:7 value:Shudipto
Immutable List
println("\n\nimmutable list of array")
for ((p,v) in list2.withIndex()){
    println("Index:$p value:$v")
}
Output:
immutable list of array
Index:0 value:1
Index:1 value:2
Index:2 value:34
Index:3 value:5
Index:4 value:6
Index:5 value:7
Index:6 value:8
Index:7 value:Shudipto
That's it on the list. Nest on set.

Set: As list, kotlin has also two types of set. one is mutable and second is immutable.
so see the code
mutable set-
val mSet = mutableSetOf(1,2,4,5,6,6,8,1)
mSet.add(5)
immutable set
val set = setOf(15,2,3,10,5,6,15,10)

mutable vs immutable set: you can update mutable set anytime. But immutable set can not update after assign.
print all the value
mutable set-
for ((p,v) in mSet.withIndex()) println("Index:$p value:$v")
output:
Index:0 value:1
Index:1 value:2
Index:2 value:4
Index:3 value:5
Index:4 value:6
Index:5 value:8
Immutable set-
for ((p,v) in set.withIndex()) println("Index:$p value:$v")
Output:
Index:0 value:15
Index:1 value:2
Index:2 value:3
Index:3 value:10
Index:4 value:5
Index:5 value:6
That's it on set.

oh! we finish all the topics. Before finish this post I will give you a gift topics.
List vs set:
you probably see that list and set is working basically same process. List and Set both save a collection of data. They also have the mutable and immutable option. you can save any time of data type and you can also mix data type. but the question is, what is the difference between list and set?
the answer is, the set is not stored duplicate value but list save. If you are working with duplicate value then you must use list. or, If you are want to ignore duplicate value then use set.

Thank you for reading.
Hope you enjoy this tutorial.
Happy coding.

About Author:

I am Shudipto Trafder,
Since 2015, I write android code. I love to implement new ideas. I use java and also kotlin. Now I am learning Flutter. I love to learn new things and also love to share. And that is the main reason to run this blog.


Let's Get Connected: Facebook Twitter Linkedin Github

No comments :